在Visual Studio中使用C#的SQL Server
我得到一个错误: System.Data.dll中发生类型为“System.Data.SqlClient.SqlException”的未处理异常在Visual Studio中使用C#的SQL Server,c#,sql,sql-server,database,visual-studio-2015,C#,Sql,Sql Server,Database,Visual Studio 2015,我得到一个错误: System.Data.dll中发生类型为“System.Data.SqlClient.SqlException”的未处理异常 有什么问题吗?我在Visual Studio上开发c#inventory management system,我正在努力解决db类的这部分问题。我认为您在填充DataTable时做错了,您不应该填充DataTable,而应该填充DataSet。试着这样做 adapter.Fill(table); 公共数据表getData(字符串procedureNa
有什么问题吗?我在Visual Studio上开发c#inventory management system,我正在努力解决db类的这部分问题。我认为您在填充DataTable时做错了,您不应该填充DataTable,而应该填充DataSet。试着这样做
adapter.Fill(table);
公共数据表getData(字符串procedureName,SqlParameter[]PROCEDUREPAMS)
{
SqlCommand=newsqlcommand();
command.CommandType=CommandType.storedProcess;
command.CommandText=procedureName;
command.Connection=连接;
if(过程程序!=null)
{
for(int i=0;i public DataTable getData(string procedureName, SqlParameter[] procedureParams)
{
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = procedureName;
command.Connection = connection;
if (procedureParams != null)
{
for (int i = 0; i < procedureParams.Length; i++)
{
command.Parameters.Add(procedureParams[i]);
}
}
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds); //Fill DataSet and try accessing your table from DataSet
return ds.Tables[0];
}
显示完整的错误消息不仅仅是它的类型请正确解释您的问题。添加一个
至少尝试捕获
块并捕获异常。如果您只需要一个表,您完全可以直接填充数据表
,而不是使用一个膨胀的数据集
,然后捕获唯一的。表[0]
从中…@marc_谢谢!!你能不能把所有的代码放到try-catch块,让我知道实际的异常情况
public DataTable getData(string procedureName, SqlParameter[] procedureParams)
{
SqlCommand command = new SqlCommand();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = procedureName;
command.Connection = connection;
if (procedureParams != null)
{
for (int i = 0; i < procedureParams.Length; i++)
{
command.Parameters.Add(procedureParams[i]);
}
}
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds); //Fill DataSet and try accessing your table from DataSet
return ds.Tables[0];
}
public DataTable getData(string procedureName, SqlParameter[] procedureParams)
{
SqlCommand command = new SqlCommand(procedureName, connection);
command.CommandType = CommandType.StoredProcedure;
if (procedureParams != null)
{
for (int i = 0; i < procedureParams.Length; i++)
{
command.Parameters.Add(procedureParams[i]);
}
}
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataTable table = new DataTable();
if (connection.State != ConnectionState.Open)
connection.Open();
adapter.Fill(table);
connection.Close();
return table;
}
command.CommandTimeout = my_timeout_variable;