ASP.NET中三种操作SQLServer方式:
SqlDataSource:
SqlDataSource sds = new SqlDataSource();
Label lbl = new Label();
sds.SelectCommandType = SqlDataSourceCommandType.Text;
sds.SelectCommand = “SELECT * FROM guidtest”;
sds.ConnectionString = “Data Source=.;Database=test;uid=sa;pwd=123″;
sds.DataSourceMode = SqlDataSourceMode.DataSet;
sds.ID = “ds”;
GridView gw = new GridView();
gw.ID = “gw”;
form1.Controls.Add(gw);
gw.DataSource = sds;
gw.DataBind();
SqlDataAdapter:
DbConnection connection = new System.Data.SqlClient.SqlConnection();
connection.ConnectionString = "Data Source=.;Database=test;uid=sa;pwd=123";
System.Data.SqlClient.SqlCommand cmd = (System.Data.SqlClient.SqlCommand)connection.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM guidtest";
System.Data.SqlClient.SqlDataAdapter sda = new System.Data.SqlClient.SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds, "guid");
GridView gw = new GridView();
gw.DataSource = ds;
gw.DataBind();
form1.Controls.Add(gw);
DbDataReader:
System.Data.Common.DbConnection conn = new System.Data.SqlClient.SqlConnection();
string connection = "Data Source=.;Database=test;uid=sa;pwd=123";
string str;
Label lbl = new Label();
conn.ConnectionString = connection;
System.Data.Common.DbCommand cmd = new System.Data.SqlClient.SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM guidtest";
cmd.Connection = conn;
conn.Open();
System.Data.Common.DbDataReader ddr = cmd.ExecuteReader() ;
while (ddr.Read())
{
lbl.Text += "Guid:" + ddr["guid"].ToString();
}
conn.Close();
form1.Controls.Add(lbl);