数据库课程实验,要求写程序实现数据库的增删改查,不限语言类型
前面实验已经使用VC实现了对数据内容的读取,今天想用C#实现,本文主要写了我过程中遇到的几个问题以及我的解决方法
insert f1; //添加窗体声明
delect f2;//删除窗体声明
update f3;//修改窗体声明
*Form1() 或 button1_Click按钮代码,实现将表内内容输出到DataGridView *
String connsql = "server=localhost.;database=MyDB;integrated security=SSPI"; // 数据库连接字符串
//database设置为自己的数据库名,以Windows身份验证
try
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = connsql;
conn.Open(); // 打开数据库连接
String sql = "select * from student"; // 查询语句
SqlDataAdapter myda = new SqlDataAdapter(sql, conn); // 实例化适配器
DataTable dt = new DataTable(); // 实例化数据表
myda.Fill(dt); // 保存数据
dataGridView1.DataSource = dt; // 设置到DataGridView中
conn.Close(); // 关闭数据库连接
}
}
catch (Exception ex)
{
MessageBox.Show("错误信息:" + ex.Message, "出现错误");
}
button2_Click按钮实现功能-单击跳转插入窗口代码(删除、修改类似)
f1 = new insert();
f1.ShowDialog();
String MyConn = "server=localhost.;database=MyDB;integrated security=SSPI";
SqlConnection MyConnection = new SqlConnection(MyConn);
string MyInsert = "insert into student(学号,姓名,性别,年龄,那啥啥,宿舍号)values('" + Convert.ToString(textBox1.Text) + "','" + Convert.ToString(textBox2.Text) + "','" + Convert.ToString(textBox3.Text) + "','" + Convert.ToString(textBox4.Text) + "','" + Convert.ToString(textBox5.Text) + "','" + Convert.ToString(textBox6.Text) + "')";
SqlCommand MyCommand = new SqlCommand(MyInsert, MyConnection);
try//异常处理
{
MyConnection.Open();
MyCommand.ExecuteNonQuery();
MyConnection.Close();
textBox1.Text = textBox2.Text = textBox3.Text = textBox4.Text = textBox5.Text = textBox6.Text = "";
MessageBox.Show("成功添加信息!");
}
catch (Exception ex)
{
Console.WriteLine("{0} Exception caught.", ex);
}
if (MessageBox.Show("是否删除该学生信息?","Confirm Message", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
String MyConn = "server=localhost.;database=MyDB;integrated security=SSPI";
SqlConnection MyConnection = new SqlConnection(MyConn);
string MyDelete = "Delete from student where 学号=" + textBox1.Text;
SqlCommand MyCommand = new SqlCommand(MyDelete, MyConnection);
try
{
MyConnection.Open();
MyCommand.ExecuteNonQuery();
MyConnection.Close();
textBox1.Text = "";
MessageBox.Show("成功删除信息!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
button1_Click代码实现点击查找信息并将信息更新到对应的textBox
String connsql = "server=localhost.;database=MyDB;integrated security=SSPI";
try
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = connsql;
conn.Open(); // 打开数据库连接
string sql = string.Format("select 姓名,性别,年龄,那啥啥,宿舍号 from student where 学号='{0}'",textBox1.Text );
SqlCommand comm = new SqlCommand(sql, conn); //command对象
SqlDataReader dr = comm.ExecuteReader(); //定义数据读取对象
if (dr.Read())
{
this.textBox2.Text = textBox1.Text;
this.textBox3.Text = (string)dr.GetValue(0);
this.textBox4.Text = (string)dr.GetValue(1);
this.textBox5.Text = (string)dr.GetValue(2);
this.textBox6.Text = (string)dr.GetValue(3);
this.textBox7.Text = (string)dr.GetValue(4);
}
conn.Close();
}
}
catch (Exception ex)
{
MessageBox.Show("错误信息:" + ex.Message, "出现错误");
}
问???
如上代码,我本来讲数据库student表中年龄、学号等信息定义为int类型,用以上语句执行查询到textBox时会报错“ 无法将类型为“System.Int32”的对象强制转换为类型“System.String” ” ,多番解决未果,故将表中类型改为nchar;
修改信息按钮实现代码
if (MessageBox.Show("是否修改该学生信息?", "Confirm Message", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
String connsql = "server=localhost.;database=MyDB;integrated security=SSPI";
SqlConnection MyConnection = new SqlConnection(connsql);
string MyUpdate = "Update student set 姓名='" + textBox3.Text + "',性别='" + textBox4.Text + "',年龄='" + textBox5.Text + "',那啥啥='" + textBox6.Text + "',宿舍号='" + textBox7.Text + "'" + " where 学号=" + "'" + textBox1.Text + "'";
SqlCommand MyCommand = new SqlCommand(MyUpdate, MyConnection);
try
{
MyConnection.Open();
MyCommand.ExecuteNonQuery();
MyConnection.Close();
textBox1.Text = textBox2.Text = textBox3.Text = textBox4.Text = textBox5.Text = textBox6.Text = textBox7.Text = "";
MessageBox.Show("成功修改信息!");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
以上便是本次实现C#对数据库增删改查功能的全部历程!
当然,我的解决办法并非最好的放法,本人只是将自己实现的过程贴出来了,如有更加优化代码写法,烦请大神们指教,小生定当感激不尽~~
本人博客该文以及实验报告下载