private void BindData()
{
string con = " Data Source=localhost; Initial Catalog=AdventureWorks ; Integrated Security=SSPI;";
DataSet ret = new DataSet();//创建数据集实例 using (SqlConnection connection = new SqlConnection(con))
{
string queryString = "select * from Sales.SalesOrderHeader where SalesOrderID<@SalesOrderID";
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand command;
command = new SqlCommand(queryString, connection);
command.CommandType = CommandType.Text;
adapter.SelectCommand = command;
adapter.SelectCommand.Parameters.Add("@SalesOrderID",SqlDbType.Int,4).Value = 43870; adapter.Fill(ret);
}
}
bindingSource1.DataSource = ret.Tables[0];
}
一直在报“未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 System.Data.dll 中”,我这段代码是看着书上写的,可是在我这里老是报错。SQL实例AdventureWorks我也安装了啊。
{
string con = " Data Source=localhost; Initial Catalog=AdventureWorks ; Integrated Security=SSPI;";
DataSet ret = new DataSet();//创建数据集实例 using (SqlConnection connection = new SqlConnection(con))
{
string queryString = "select * from Sales.SalesOrderHeader where SalesOrderID<@SalesOrderID";
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand command;
command = new SqlCommand(queryString, connection);
command.CommandType = CommandType.Text;
adapter.SelectCommand = command;
adapter.SelectCommand.Parameters.Add("@SalesOrderID",SqlDbType.Int,4).Value = 43870; adapter.Fill(ret);
}
}
bindingSource1.DataSource = ret.Tables[0];
}
一直在报“未处理的“System.Data.SqlClient.SqlException”类型的异常出现在 System.Data.dll 中”,我这段代码是看着书上写的,可是在我这里老是报错。SQL实例AdventureWorks我也安装了啊。
为数据库连接语句,你把用户名和数据库名,密码都写对着吗?
它的注释说adapter.SelectCommand.Parameters.Add("@SalesOrderID",SqlDbType.Int,4).Value = 43870; 是添加查询命令中的参数值,在书中三个示例都出现了这句话。
一般来讲应该是可以运行的。
加个空格
string con = " Data Source=localhost; Initial Catalog=AdventureWorks ; Integrated Security=SSPI;";
修改后:
string con = @" Data Source=localhost; Initial Catalog=AdventureWorks ; Integrated Security=SSPI;";应该要用个@符号转义的……
我后来重新建了一个工程,把代码粘贴进去,可以正常运行。
还是谢谢各位的热心帮助!