c# - sql server - C程序不执行存储过程

存储过程有两个输入参数,并基于这些信息将信息插入到表中,

在SQL-Server中使用my SP as


exec Commentary.Add_Information @ID = 34926, @year = '2020' 



工作的很好。 但是从c#执行它不会在表中输入


public void CreateBasicInformation()


{


 SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();


 builder.DataSource ="myServer.database.windows.net";


 builder.UserID ="admin";


 builder.Password ="myPass";


 builder.InitialCatalog ="myDB";



 try


 {


 SqlConnection cn = new SqlConnection(builder.ConnectionString);


 cn.Open();


 SqlCommand cmd = new SqlCommand("Add_Information", cn);


 cmd.CommandType = CommandType.StoredProcedure;


 cmd.Parameters.AddWithValue("@ID","34926");


 cmd.Parameters.AddWithValue("@year","2020");


 cn.Close();


 }


 catch (SqlException sqlEx)


 {


 Console.WriteLine(sqlEx.Message);


 }


 catch (Exception ex)


 {


 Console.WriteLine(ex.Message);


 }


}




The program '[9480] test_SP.exe' has exited with code 0 (0x0).



时间: 作者:


cmd.ExecuteNonQuery();


cn.Close();



现在强烈建议使用Using statement

作者:

你没有执行命令: https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.executenonquery?view=netframework-4.8

try


public void CreateBasicInformation()


{


 SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();


 builder.DataSource ="myServer.database.windows.net";


 builder.UserID ="admin";


 builder.Password ="myPass";


 builder.InitialCatalog ="myDB";



 try


 {


 SqlConnection cn = new SqlConnection(builder.ConnectionString);


 cn.Open();


 SqlCommand cmd = new SqlCommand("Add_Information", cn);


 cmd.CommandType = CommandType.StoredProcedure;


 cmd.Parameters.AddWithValue("@ID","34926");


 cmd.Parameters.AddWithValue("@year","2020");


 // Execute and get rows affected count. 


 var rowsAffected = cmd.ExecuteNonQuery();


 cn.Close();


 }


 catch (SqlException sqlEx)


 {


 Console.WriteLine(sqlEx.Message);


 }


 catch (Exception ex)


 {


 Console.WriteLine(ex.Message);


 }


}



作者:
...