Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/rest/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Visual Studio中使用C#的SQL Server_C#_Sql_Sql Server_Database_Visual Studio 2015 - Fatal编程技术网

在Visual Studio中使用C#的SQL Server

在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

我得到一个错误:

System.Data.dll中发生类型为“System.Data.SqlClient.SqlException”的未处理异常


有什么问题吗?我在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;