日期:2014-05-17 浏览次数:20793 次
protected void Button1_Click(object sender, EventArgs e)
{
string StrCon = "Data Source=localhost;Initial Catalog=systemDB;" + "Integrated Security=True;User ID=sa;Password=";
SqlConnection conn = new SqlConnection(StrCon);
//定义SQL字符串
string MySQL = "Select * From Teacher;";
//定义数据适配器对象
SqlDataAdapter MyAdapter = new SqlDataAdapter(MySQL, conn);
//打开数据库连接
conn.Open();
//定义MyAdapter对象的UpdateCommand属性使用的Update语句
MyAdapter.UpdateCommand = new SqlCommand("Update Teacher set " +
"CourseName=@CourseName where TeacherName=@TeacherName", conn);
//定义UpdateCommand属性使用的参数名称、类型、长度、使用的表的字段名称,需要和表中的定义吻合
MyAdapter.UpdateCommand.Parameters.Add("@CourseName", SqlDbType.VarChar, 20, "CourseName");
//定义一个参数对象,并给其赋值
SqlParameter parameter = MyAdapter.UpdateCommand.Parameters.Add("@TeacherName", SqlDbType.VarChar);
parameter.SourceColumn = "TeacherID";//SourceColumn属性,获取或设置源列的名称,该源列映射到 DataSet 并用于加载或返回 Value
parameter.SourceVersion = DataRowVersion.Original;//SourceVersion属性,获取或设置在加载 Value 时要使用的 DataRowVersion;DataRowVersion描述 DataRow 的版本,Original 该行中包含其原始值
//定义数据集对象
DataSet MyDataSet = new DataSet();
//用表studentinfo的内容填充数据集对象
MyAdapter.Fill(MyDataSet, "Teacher");
//定义数据行对象
DataTable MyTable = MyDataSet.Tables["Teacher"];
foreach (DataRow MyRow in MyTable.Rows)
{
if (MyRow["TeacherName"].ToString() == Session["UserName"].ToString())
{
MyRow["CourseName"] = DropDownList1.SelectedIndex;
MyAdapter.Upda