2010-08-25 104 views
0

我想用ASP.NET连接到SQL Server。任何人都可以给我一些样例程序吗?如何使用asp.net连接到SQL Server

+1

不是负面的,但这似乎是一个微不足道的问题,通过文档和谷歌搜索回答。 – Pondlife 2010-08-25 07:26:03

+0

@Pondlife。你可以通过谷歌搜索来回答几乎任何问题:) – hgulyan 2010-08-25 09:07:45

+0

@hgulyan。绝对正确:-)但这个问题是如此的基本,只是不值得在这里发布(恕我直言)。 – Pondlife 2010-08-25 09:52:59

回答

0

June R的例子是插入数据。

using System.Data; 
using System.Data.SqlClient; 

string Connection = "server=ALDAN; uid=sa; pwd=sa; database=GAZCAD; Connect Timeout=10000"; 

SqlConnection DataConnection = new SqlConnection(Connection); 
// the string with T-SQL statement, pay attention: no semicolon at the end of //the statement 
string Command = "Select * from myTable"; 
// create the SQLCommand instance 
SQLCommand DataCommand = new SqlCommand(Command, DataConnection); 
// open the connection with our database 
DataCommand.Connection.Open(); 
// Data adapter 
SqlDataAdapter da = new SqlDataAdapter(DataCommand); 
// To fill data, i need a datatable. 
DataTable dt = new DataTable(); 
// Filling my table 
da.Fill(dt);