到目前为止,我设法使用“插入”命令在名为 "学生" 的表格中创建新记录。这是我使用的代码:
int ID = int.Parse( TextBox1.Text);
string name = TextBox2.Text;
string gender = TextBox3.Text;
int marks = int.Parse(TextBox4.Text);
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
//create data adapter
SqlDataAdapter adapter = new SqlDataAdapter("select * from students",con);
//create sqlcommand to store execute stored procedure
adapter.InsertCommand = new SqlCommand("spInsertStudent",con);
adapter.InsertCommand.CommandType = CommandType.StoredProcedure;
//create parameter for Return value (@ROWCOUNT)
SqlParameter parameter = adapter.InsertCommand.Parameters.Add("@ROWCOUNT",SqlDbType.Int);
parameter.Direction = ParameterDirection.ReturnValue;
adapter.InsertCommand.Parameters.Add("@ID", SqlDbType.Int, 0, "ID");
adapter.InsertCommand.Parameters.Add("@Name",SqlDbType.NVarChar,50,"Name");
adapter.InsertCommand.Parameters.Add("@Gender",SqlDbType.NVarChar,10,"Gender");
adapter.InsertCommand.Parameters.Add("@TotalMarks",SqlDbType.Int,0,"TotalMarks");
DataSet ds = new DataSet();
//DataTable students = new DataTable();
adapter.Fill(ds,"Students");
DataTable students = ds.Tables["Students"];
DataRow studentRow = students.NewRow();
studentRow["ID"] = ID;
studentRow["Name"] = name;
studentRow["Gender"] = gender;
studentRow["TotalMarks"] = marks;
students.Rows.Add(studentRow);
adapter.Update(ds,"Students");
现在我想编辑一个数据行。如果ID与ID参数匹配,则数据将更改。这是我使用的存储过程:
create procedure updateStudent
@ID int,
@Name varchar(50),
@Gender varchar(10),
@TotalMarks int
AS
BEGIN
update Students set Name = @Name, Gender = @Gender, TotalMarks = @TotalMarks
where ID = @ID
END
这是我在后面的代码中使用的C#代码:
int ID = int.Parse(TextBox1.Text);
string name = TextBox2.Text;
string gender = TextBox3.Text;
int marks = int.Parse(TextBox4.Text);
string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlDataAdapter adapter = new SqlDataAdapter("Select * from Students",con);
DataTable dtStudents = new DataTable();
adapter.Fill(dtStudents);
SqlCommand cmd = new SqlCommand("updateStudent",con);
cmd.Parameters.Add("@ID", SqlDbType.Int, 0, "ID").Value = ID;
cmd.Parameters.Add("@Name", SqlDbType.NVarChar, 50, "Name").Value = name;
cmd.Parameters.Add("@Gender", SqlDbType.NVarChar, 10, "Gender").Value = gender;
cmd.Parameters.Add("@TotalMarks", SqlDbType.Int, 0, "TotalMarks").Value = marks;
adapter.UpdateCommand = cmd;
adapter.Update(dtStudents);
}
但是在我点击网络表单上的“更新”按钮后,没有错误消息,数据也没有在我的数据库中更新。我在这里做错了什么?
答案 0 :(得分:0)
我认为你应该编写用于执行存储过程的类。它将更容易阅读和处理。像DB
这样的类中的RunQuery方法 private SqlConnection Connection;
public bool Open()
{
try
{
ConnectionString = "your connection string";
Connection = new SqlConnection(ConnectionString);
Connection.Open();
return true;
}
catch (Exception ex)
{
errorMessage = ex.Message;
return false;
}
}
public bool RunQuery(string ProcedureName, SqlParameter[] Parameters)
{
bool res = false;
try
{
SqlCommand Command = new SqlCommand();
Command.CommandText = ProcedureName;
Command.CommandType = CommandType.StoredProcedure;
Command.Connection = Connection;
Command.Parameters.AddRange(Parameters);
Command.ExecuteNonQuery();
res = true;
}
catch (Exception ex)
{
throw ex;
}
return res;
}
你可以称之为
SqlParameter[] param= new SqlParameter[4];
param[0] = new SqlParameter("@ID", ID);
param[1] = new SqlParameter("@Name", name);
param[2] = new SqlParameter("@Gender", gender);
param[3] = new SqlParameter("@TotalMarks", marks);
RunQuery("updateStudent", param);