我在 C# 控制台应用程序中执行存储过程时遇到了问题,但不知道具体的问题出在哪里。能否请你看一下?
string path="";
StringBuilder sb = new StringBuilder();
StringBuilder sqlErrorMessages = new StringBuilder("Sql Exception:\n");
try
{
SqlConnection conn = new SqlConnection("Data Source=DESKTOP-M3IMRLE\\SQLEXPRESS; Initial Catalog = db2; Integrated security=true");
Console.WriteLine("Enter path : ");
path = Console.ReadLine();
conn.Open();
SqlCommand cmd = new SqlCommand();
SqlCommand command = new SqlCommand("EXECUTE main.mainproc @path='" + path + "'", conn);
if(command!=null)
{
Console.WriteLine("JSON loaded");
}
conn.Close();
}
catch(SqlException ex)
{
sqlErrorMessages.AppendFormat("Message: {0}\n", ex.Message);
}
cmd
和sb
,但没有使用它们,它们可能可以被删除。您应该将命令的 CommandText 声明为存储过程的名称,并将 CommandType 设置为 CommandType.StoredProcedure,然后将路径值作为 SqlParameter 添加到命令中。 - Jonathon Chase