protected void Page_Load(object sender, EventArgs e)
{
string dataSource = "Data Source=localhost";
string security = "user id=sa;password=;";
string initialCatalog = "initial catalog=pubs;";
string cnnString = dataSource + security + initialCatalog; SqlConnection connection = new SqlConnection(cnnString); //数据库连接
string strSql = "select * from [authors]";
SqlCommand cmd = new SqlCommand(strSql, connection); DataSet dataSet = new DataSet(); try
{
connection.Open();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
sda.Fill(dataSet);//dataSet为空,为什么会这样呢?
}
catch (SqlException ex)
{
Response.Write(ex.ToString());
}
finally
{
connection.Close();
} grid1.DataSource=dataSet.Tables[0];
grid1.DataBind();
}
}
{
string dataSource = "Data Source=localhost";
string security = "user id=sa;password=;";
string initialCatalog = "initial catalog=pubs;";
string cnnString = dataSource + security + initialCatalog; SqlConnection connection = new SqlConnection(cnnString); //数据库连接
string strSql = "select * from [authors]";
SqlCommand cmd = new SqlCommand(strSql, connection); DataSet dataSet = new DataSet(); try
{
connection.Open();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
sda.Fill(dataSet);//dataSet为空,为什么会这样呢?
}
catch (SqlException ex)
{
Response.Write(ex.ToString());
}
finally
{
connection.Close();
} grid1.DataSource=dataSet.Tables[0];
grid1.DataBind();
}
}
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dataSet); //断点调试下 就知道哪一步有问题了
改成
string dataSource = "Data Source=localhost;"; 少了个分号 连接字符串建议放到webconfig中