如何使用asp.net连接到SQL Server

时间:2010-08-25 06:13:39

标签: asp.net sql sql-server

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

2 个答案:

答案 0 :(得分:2)

答案 1 :(得分: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);