使用 VS2015/C 中的存储过程创建一个 ADO.NET 只读 SQL 服务器数据集#

Create an ADO.NET read only SQL Server data set using a stored procedure in VS2015/C#

我想使用 C# 在 VS2015 中使用存储过程创建一个 ADO.NET 只读 SQL 服务器数据集。有没有办法做到这一点?

问题是当我尝试创建命令对象来设置我要使用的存储过程时,它会抛出错误。我知道我遗漏了一些东西,但我无法在 MS 帮助文件中找到任何内容来举例说明如何执行此操作。我不想使用 Linq!您可以提供的任何帮助将不胜感激。

try
{
     string connString = AdoHelper.ConnectionString;

     var myConnection = new SqlConnection(connString);
     CommandType myCommand = System.Data.CommandType.StoredProcedure;

     using (myConnection)
     {
          myConnection.Open();
          // myCommand. = myConnection;
          // myCommand.CommandType = CommandType.StoredProcedure;
          // myCommand.CommandTimeout = 540;

          if (this.optInStockOnly.Checked == true)
          {
              myCommand.CommandText = "InventoryGetLookupDataInStockOnly"; // Stored procedure Name
          }
          else
          {
              myCommand.CommandText = "InventoryGetLookupData";     // Stored procedure name 
          }

          myCommand.Parameters.AddWithValue(parameterName: "@CurrentWareHouseCode", value: MyGlobals.CurrentUsersInfo.CurrentUserWarehousecode);

          SqlDataReader reader = myCommand.ExecuteReader();

          if (reader.Read())
          {
              // set recordset here and do rest of the stuff I want
          }
    }
}

如评论中所述,您没有link连接对象的命令。

看看这个例子:

private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand();
        command.Connection = connection;
        command.CommandTimeout = 15;
        command.CommandType = CommandType.Text; // CommandType.StoredProcedure; in case this was a Proc
        command.CommandText = queryString; // Proc name

        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            Console.WriteLine(String.Format("{0}, {1}",
                reader[0], reader[1]));
        }
    }
}

更多关于this