我想在带有C#的VS2015中使用存储过程创建ADO只读SQL数据记录集。有没有办法做到这一点?
我正在使用的代码是。问题是,当我尝试创建Command对象来设置要使用的存储过程时,会引发错误。我知道我遗漏了一些东西,但是我无法在MS帮助文件中找到任何提供有关如何执行此操作的示例的信息。我不想使用Linq!您能提供的任何帮助将不胜感激。我已经评论了MyCommand Lines。
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
}
如注释中所述,您没有将命令链接到连接对象。
查看此示例:
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的更多信息>