Ⅰ vb中连接sql查询语句代码
‘模块中的代码
Public Function connectstr() As String
sqlpath = App.Path & "\数据库.mdb"
connectstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sqlpath & "" ‘access数据库
End Function
Public Function runselect(ByVal sqlm As String) As ADODB.Recordset '返回记录集
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
On Error GoTo run_error
Set cn = New ADODB.Connection
cn.Open connectstr()
Set rs = New ADODB.Recordset
rs.CursorLocation = adUseClient
rs.LockType = adLockOptimistic
rs.Open Trim(sqlm), cn, adOpenDynamic
Set runselect = rs
Set rs = Nothing
Set cn = Nothing
run_error:
MsgBox Err.Description
End Function
’窗体中的代码.................
Private Sub Combo1_Change()
Dim mrc As New ADODB.Recordset
Set mrc = runselect("select * from 表 where 字段='" & Trim(Combo1.Text) & "'")
Set DataGrid1.DataSource = mrc '显示在 datagrid 中
End Sub
Ⅱ 关于sql查询代码
select answer1,count(*) AS 数量 from [数据库表名] where answer1 in ('A', 'B', 'C' ,'D') group by answer1
显示的话, 只要输出就可以了
Ⅲ sql连接数据库服务器代码
OLEDB的连接串?
Provider=SQLOLEDB;Server=Localhost;database=master;User ID=sa;Password=;
Localhost是指本机数据库,可以改成数据库所在的服务IP地址。
master是SQL主数据库,要改成你的数据库名。
sa 是用户登录名,密码则是相对应登录密码。
Ⅳ java实现sql连接和查询的代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
public class DBCon {
//数据库驱动对象
public static final String DRIVER="oracle.jdbc.driver.OracleDriver";
//数据库连接地址(数据库名)
public static final String URL="jdbc:oracle:thin:@localhost:1521:orcl";
//登陆名
public static final String USER="FM";
//登陆密码
public static final String PWD="FM";
//创建数据库连接对象
private Connection con=null;
//创建数据库预编译对象
private PreparedStatement ps=null;
//创建结果集
private ResultSet rs=null;
//创建数据源对象
public static DataSource source=null;
// //静态代码块
// static{
//
// //初始化配置文件context
// try {
// Context context=new InitialContext();
// source=(DataSource)context.lookup("java:comp/env/jdbc/webmessage");
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
//
// }
/**
* 获取数据库连接
*/
public Connection getCon(){
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
con=DriverManager.getConnection(URL,USER,PWD);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return con;
}
// /**
// * 获取数据库连接
// */
// public Connection getCon(){
//
// try {
// con=source.getConnection();
// } catch (SQLException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
// return con;
// }
/**
* 关闭所有资源
*/
public void closeAll(){
if(rs!=null)
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(ps!=null)
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(con!=null)
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* @param sql数据库更新(增、删、改) 语句
* @param pras参数列表(可传,可不传,不传为NULL,以数组形式存在)
* @return 返回受影响都行数
*/
public int update(String sql,String... pras){
int resu=0;
con=getCon();
try {
ps=con.prepareStatement(sql);
for(int i=0;i<pras.length;i++){
ps.setString(i+1,pras[i]);
}
resu=ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
closeAll();
}
return resu;
}
/**
* @param sql数据库查询语句
* @param pras参数列表(可传,可不传,不传为NULL,以数组形式存在)
* @return 返回结果集
*/
public ResultSet query(String sql,String... pras){
con=getCon();
try {
ps=con.prepareStatement(sql);
if(pras!=null)
for(int i=0;i<pras.length;i++){
ps.setString(i+1, pras[i]);
}
rs=ps.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return rs;
}
}
Ⅳ sql多表连接查询结果集拼接三个字段和固定值再添加到别的表中
insertinto别的表(col1,col2,col3)
select
a.col1
,b.col1
,c.col1
from
表1a
innerjoin表2bona.id=b.id
innerjoin表3cona.id=c.id
由于未给出数据结构,故以上代码为示意代码
Ⅵ asp代码中SQL查询语句的连接方法
试一下这个行不行,我怀疑的是字符串连接问题conn.execute("insert into test (title,content,user,subtime) values('"&title&"','"&content&"','"&user&"',now())") 不行的话。里面的user 好像是关键字用这个包括起来[user]
Ⅶ sql 查询代码
以查询价格为例:
<%
set conn=server.createobject("adodb.connection")
conn.open "driver={microsoft access driver (*.mdb)};dbq="&server.mappath("..\date\qfx_qfxt.mdb")
set rs=server.createobject("adodb.recordset")
exec="select * from cellphone where 价格字段名='某价格'"
rs.open exec,conn,1,1
response.write rs("价格字段名")
%>
Ⅷ SQL Server 2012 多表连接查询功能实例代码
废话不多说了,直接给大家贴代码了,具体代码如下所示:
--
交叉连接产生笛卡尔值
(X*Y)
SELECT
*
FROM
Student
cross
Join
dbo.ClassInfo
--另外一种写法
SELECT
*
FROM
Student
,
ClassInfo
--
内连接
(Inner
可以省略)
SELECT
*
FROM
Student
JOIN
dbo.ClassInfo
ON
dbo.Student.Class
=
dbo.ClassInfo.ID;
--
Inner
Join
SELECT
*
FROM
Student
INNER
JOIN
dbo.ClassInfo
ON
dbo.Student.Class
=
dbo.ClassInfo.ID;
--
on
条件,通常是
主外键,但是不限于主外键
--
on
条件,允许有多个,而且可能是针对某个表的
SELECT
*
FROM
Student
INNER
JOIN
dbo.ClassInfo
ON
dbo.Student.Class
=
dbo.ClassInfo.ID
AND
dbo.Student.Class
IN
(1,2)
--
针对Student表增加查询条件
--
不等于
(笛卡尔值减去
相等的值)
SELECT
*
FROM
Student
INNER
JOIN
dbo.ClassInfo
ON
dbo.Student.Class
<>
dbo.ClassInfo.ID;
--自连接
从Class表中查询到Class所在的系
SELECT
T1.*
,T2.ClassName
FROM
dbo.ClassInfo
AS
T1
INNER
JOIN
dbo.ClassInfo
AS
T2
ON
T1.PID=T2.ID
--
Left
Join
SELECT
*
FROM
Student
Left
JOIN
dbo.ClassInfo
ON
dbo.Student.Class
=
dbo.ClassInfo.ID;
--
Right
Join
SELECT
*
FROM
Student
RIGHT
JOIN
dbo.ClassInfo
ON
dbo.Student.Class
=
dbo.ClassInfo.ID;
--
全外连接
SELECT
*
FROM
Student
FULL
OUTER
JOIN
dbo.ClassInfo
ON
dbo.Student.Class
=
dbo.ClassInfo.ID;
--
外连接和内连接的区别是:
--
内连接,on条件不符合的话,会过滤掉数据
--
外连接,以保留表为主,on条件成立显示数据,否则显示NULL
--Union
纵向连接
SELECT
StuID,StuName,StuEnName,StuAge,StuBirthday
FROM
Student
WHERE
StuID<=2
UNION
SELECT
StuID,StuName,StuEnName,StuAge,StuBirthday
FROM
dbo.Student
WHERE
StuID>2
--Union
去重复
SELECT
StuSex
FROM
Student
WHERE
StuID<=2
UNION
SELECT
StuSex
FROM
dbo.Student
WHERE
StuID>2
--Union
显示全部
SELECT
StuSex
FROM
Student
WHERE
StuID<=2
UNION
ALL
SELECT
StuSex
FROM
dbo.Student
WHERE
StuID>2
--Except
差集,排除
SELECT
StuID,StuName,StuEnName,StuAge,StuBirthday
FROM
Student
Except
SELECT
StuID,StuName,StuEnName,StuAge,StuBirthday
FROM
dbo.Student
WHERE
StuID<=2
--Intersect
交集
SELECT
StuID,StuName,StuEnName,StuAge,StuBirthday
FROM
Student
WHERE
StuID>=2
Except
SELECT
StuID,StuName,StuEnName,StuAge,StuBirthday
FROM
dbo.Student
WHERE
StuID<=3
以上所述是小编给大家介绍的SQL
Server
2012
多表连接查询功能实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!
Ⅸ sql本地和远程连接代码
Dim SqlDatabaseName,SqlPassword,SqlUsername,SqlLocalName,ConnStr,Conn
SqlDatabaseName = "SQL数据库名"
SqlUsername = "SQL数据库用户名"
SqlPassword = "SQL数据库用户密码"
SqlLocalName = "SQL主机IP地址" 'SQL主机IP地址(本地可用“127.0.0.1”或“(local)”,非本机请用真实IP)
ConnStr = "Provider = Sqloledb; User ID = " & SqlUsername & "; Password = " & SqlPassword & "; Initial Catalog = " & SqlDatabaseName & "; Data Source = " & SqlLocalName & ";"
On Error Resume Next
Set conn = Server.CreateObject("ADODB.Connection")
conn.open ConnStr