我的存储过程是:
ALTER PROCEDURE [dbo].[ThemTram]
(
@Ten nvarchar(50),
@KhuVucID int,
@TenTinh nvarchar(50),
@TinhID int,
@TenTram nvarchar(50)
)
AS
set @KhuVucID = (select KhuVuc.KhuVucID from KhuVuc where KhuVuc.Ten=@ten)
set @TinhID =(select Tinh.TinhID from Tinh Where (Tinh.TenTinh=@TenTinh and Tinh.KhuVucID=@KhuVucID))
if not exists (select Tram.TenTram from Tram where (Tram.TenTram=@TenTram and Tram.TinhID = @TinhID))
Begin
insert into Tram(TenTram,TinhID) values (@TenTram,@TinhID)
end
和我的 C# 程序:
private void ThemTramBT_Click(object sender, EventArgs e)
{
string conn = "Data Source=USER-PC;Initial Catalog=NCKHmoi;Integrated Security=True";
SqlConnection connect = new SqlConnection(conn);
SqlCommand command = new SqlCommand();
command.Connection = connect;
connect.Open();
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "ThemTram";
command.Parameters.Add("@Ten", SqlDbType.NVarChar, 50).Value = KhuVucComboBox.Text;
command.Parameters.Add("@TenTinh", SqlDbType.NVarChar, 50).Value = TinhComboBox.Text;
command.Parameters.Add("@TenTram", SqlDbType.NVarChar, 50).Value = ThemTramTB.Text;
command.Dispose();
connect.Close();
}
但有一个问题。在 c# 代码中。我无法向其中插入新数据。请帮我。