當前位置:首頁 » 編程語言 » sql2014連接查詢代碼
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

sql2014連接查詢代碼

發布時間: 2022-03-05 20:11:16

Ⅰ 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