言語:C#
スキル:初心者
ツール:Visual Studio 2010
こんにちは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)