语言:C#
技巧:初学者
工具:Visual Studio 2010
Hello StackOverflow,
我已经制作了一个用于将数据插入表格的存储过程......
当我在服务器资源管理器中执行存储过程并且它插入到该表中时,存储过程正常工作...
但是当我从我的代码中调用这个存储过程时,我得到输出“1行受影响”但没有数据插入到表中......!
我很困惑发生了什么事。
private void btn_AssignTeacher_Click(object sender, EventArgs e)
{
bool result = false;
SqlCommand cmd = new SqlCommand("sprocInsert");
int teacherid = comboBox_teachername.SelectedIndex +1;
int coursesinfoid = comboBox_coursename.SelectedIndex+1;
int daysid = comboBox_day.SelectedIndex+1;
int slotsid = comboBox_slot.SelectedIndex+1;
cmd.Parameters.Add("@coursesinfoid", SqlDbType.Int).Value = coursesinfoid;
cmd.Parameters.Add("@daysid", SqlDbType.Int).Value = daysid;
cmd.Parameters.Add("@slotsid", SqlDbType.Int).Value = slotsid;
cmd.Parameters.Add("@teacherid", SqlDbType.Int).Value = teacherid;
result = AssignCoursesMgr.Insert(cmd);
if (result == true)
MessageBox.Show("teacher assign successfully");
}
class AssignCoursesManager
{
DBLayer DBworks;
public bool Insert(SqlCommand cmd)
{
DBworks = new DBLayer();
cmd.CommandType = System.Data.CommandType.StoredProcedure;
return DBworks.InsertData(cmd);
}
}
public bool InsertData(SqlCommand cmd)
{
int result = 0;
SqlConnection conn = CreateConnection(ConnectionString);
try
{
cmd.Connection = conn;
cmd.Connection.Open();
result = cmd.ExecuteNonQuery();
}
catch (SqlException exc)
{
MessageBox.Show(exc.Message);
}
finally
{
conn.Close();
cmd.Dispose();
}
if (result > 0)
return true;
else return false;
}
这是“sprocInsert”
ALTER PROCEDURE dbo.sprocInsert
(
@coursesinfoid int,
@teacherid int,
@daysid int,
@slotsid int
)
AS
INSERT INTO schedule (coursesinfoid,teacherid,daysid,slotsid)
VALUES(@coursesinfoid,@teacherid,@daysid,@slotsid)