我一次在两个表中插入数据......
CREATE PROCEDURE inserttwo
(
@BookID int,
@BookName nvarchar(50),
@DateIssue datetime,
@ReturnDate datetime,
@PersonID int
)
AS
INSERT INTO tblReturn(BookID, BookName, DateIssue, ReturnDate, PersonID)
VALUES(@BookID, @BookName, @DateIssue, @ReturnDate, @PersonID)
INSERT INTO tblIssue(BookID, BookName, DateIssue, ReturnDate, PersonID)
VALUES(@BookID, @BookName, @DateIssue, @ReturnDate, @PersonID)
然后我按存储过程更新和删除这些表...
删除查询:
ALTER PROCEDURE [dbo].[Issuedelete]
(@BookID int)
AS
DELETE FROM tblIssue
WHERE BookID = @BookID
更新查询:
ALTER PROCEDURE [dbo].[IssueUpdate]
(@BookID int,
@BookName nvarchar(50),
@DateIssue datetime,
@ReturnDate datetime,
@PersonID int)
AS
UPDATE tblIssue
SET [BookID] = @BookID ,
[BookName] = @BookName,
[DateIssue] = @DateIssue,
[ReturnDate] = @ReturnDate,
[PersonID] = @PersonID
WHERE BookID = @BookID
要从tblIssue中删除的C#代码:
private void btnDelete_Click(object sender, EventArgs e)
{
try
{
string c = ConfigurationManager.ConnectionStrings["LMS"].ConnectionString;
SqlConnection con = new SqlConnection(c);
con.Open();
SqlCommand cmd = new SqlCommand("Issuedelete", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@BookID", ComBox1BookID.Text);
cmd.ExecuteNonQuery();
con.Close();
storedproc();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
更新tblIssue的C#代码:
private void btnupdate_Click(object sender, EventArgs e)
{
try
{
string c = ConfigurationManager.ConnectionStrings["LMS"].ConnectionString;
SqlConnection con = new SqlConnection(c);
con.Open();
SqlCommand cmd = new SqlCommand("IssueUpdate", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@BookID", ComBox1BookID.Text);
cmd.Parameters.AddWithValue("@BookName", ComBox2BName.Text);
cmd.Parameters.AddWithValue("@DateIssue", IssueDate.Value.ToString());
cmd.Parameters.AddWithValue("@ReturnDate", ReturnDate.Value.ToString());
cmd.Parameters.AddWithValue("@PersonID", CBox3PerID.Text);
cmd.ExecuteNonQuery();
con.Close();
storedproc();
}
catch (Exception ex)
{
Console.WriteLine("SqlError" + ex);
}
}
编译后,我收到错误
无法将数据类型转换为int
我尝试使用OleDB和SqlDbType更改参数的其他方法...但是它没有删除和更新记录......当我更新和删除到tblReturn时也遇到同样的问题。 。 请帮我??? :(
答案 0 :(得分:1)
您要将文本值发送到整数和日期时间参数,将更新按钮单击事件代码更改为
private void btnupdate_Click(object sender, EventArgs e)
{
try
{
string c = ConfigurationManager.ConnectionStrings["LMS"].ConnectionString;
SqlConnection con = new SqlConnection(c);
con.Open();
SqlCommand cmd = new SqlCommand("IssueUpdate", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@BookID", Convert.ToInt32(ComBox1BookID.Text));
cmd.Parameters.AddWithValue("@BookName", ComBox2BName.Text);
cmd.Parameters.AddWithValue("@DateIssue", IssueDate.Value);
cmd.Parameters.AddWithValue("@ReturnDate", ReturnDate.Value);
cmd.Parameters.AddWithValue("@PersonID",Convert.ToInt32(CBox3PerID.Text));
cmd.ExecuteNonQuery();
con.Close();
storedproc();
}
catch (Exception ex)
{
Console.WriteLine("SqlError" + ex);
}
}
<强>更新强> 在更新存储过程中 set 之后删除此行
[BookID] = @ BookID,
如果是标识列,则在更新时会抛出异常。
更新2
试试这个cmd.Parameters.Add("@BookID", SqlDbType.Int).Value = Convert.ToInt32(ComBox1BookID.Text);
如果要添加添加功能的参数