通过SqlDataAdapter操作DataSet修改数据库和其余的数据库操作大致都是一样的,主要是语法的不同,如DataSet的使用,都是使用SqlDataAdapter将数据填充到DataSet中,然后操作DataSet时使用的是行和列的操作,比如一行的操作ds.Tables["student"].Rows[0].Delete();行中的一列则可以使用ds.Tables["student"].Rows[0]["Name"] = name;来操作。
ADO.NET包括了几个组件类:SqlConnection、SqlCommand、SqlDataAdapter、SqlDataReader、DataSet、DataTable。
先写几个功能:通过SqlDataAdapter操作DataSet修改数据库,代码如下:
DataSet ds = new DataSet();
string strConnection = Properties.Settings.Default.sqlConnection;
SqlConnection conn = new SqlConnection(strConnection);
conn.Open();
string name = textBox2.Text;
string age = textBox3.Text;
string id = textBox1.Text;
SqlDataAdapter sda = new SqlDataAdapter("select * from student where id =" + id + "", conn); //必须使用select返回一个主键或者一个列属性
SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(sda);
// string updateCommand = cmdBuilder.GetUpdateCommand().CommandText;
string deleteCommand = cmdBuilder.GetDeleteCommand().CommandText;
sda.Fill(ds,"student");
/*
ds.Tables["student"].Rows[0]["Name"] = name; //更新操作,一列
ds.Tables["student"].Rows[0]["Age"] = age;
sda.Update(ds,"student"); //更新数据库
conn.Close();
*/
ds.Tables["student"].Rows[0].Delete(); //删除操作,一行
sda.Update(ds, "student"); //更新数据库
conn.Close();
通过SqlDataAdapter操作DataSet修改数据库,插入一条记录,代码如下:
string strconn = Properties.Settings.Default.sqlConn;
using (SqlConnection conn = new SqlConnection(strconn))
{
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select * from student1";
cmd.CommandType = CommandType.Text;
cmd.Connection = conn;
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommandBuilder build = new SqlCommandBuilder(adapter);
DataSet ds = new DataSet();
adapter.SelectCommand = cmd;
adapter.Fill(ds);
DataRow row = ds.Tables[0].NewRow();
row["id"] = "4";
row["name"] = "jjf";
row["age"] = "14";
ds.Tables[0].Rows.Add(row);
adapter.Update(ds);
adapter.Dispose();
cmd.Dispose();
conn.Dispose();
}
使用SqlDataReader,代码如下:
string sqlConn = Properties.Settings.Default.sqlConnection;
SqlConnection conn = new SqlConnection(sqlConn);
conn.Open();
SqlCommand command = conn.CreateCommand();
string id = textBox1.Text;
command.CommandText = "select * from student where Id = "+id+"";
SqlDataReader sdr = command.ExecuteReader(); //执行查询显示用SqlDataReader sdr = command.ExecuteReader()
if (sdr.Read())
{
textBox1.Text = sdr["id"].ToString(); // textBox1.Text = sdr[0].ToString();
textBox2.Text = sdr["name"].ToString();
textBox3.Text = sdr["age"].ToString();
sdr.Close();
将数据库中的信息显示在DataGridView上,代码如下:
用于将数据库中表的信息显示到DataGridView上的数据操作,这个数据库的操作和增删改查的不一样,函数和参数有变化,SqlCommand的不一样。
string strConnection = Properties.Settings.Default.SqlConnection;
SqlConnection conn = new SqlConnection(strConnection);
//conn.Open();
string sqlcommand = "select * from student";
SqlCommand command = new SqlCommand(sqlcommand,conn);//初始化具有查询文本和连接的SqlCommand新实例。
SqlDataAdapter sda = new SqlDataAdapter(command);//初始化SqlDataAdapter类的新实例,用指定的SqlCommand实例作为参数的属性。
DataSet ds = new DataSet(); //
sda.Fill(ds); //把数据库中的数据通过SqlDataAdapter对象填充DataSet。
dataGridView1.DataSource = ds.Tables[0]; //获取包含在 System.Data.DataSet 中的表的集合。注意数组的下标
conn.Close();
Sql常用命令:
写SQL语句时为字符串,但是其中有变量时则需要用+来将其括起来,其他的任何都用双引号引起来。在最后面如果是一个用+括起来的变量,则最后面必须用“”,因为它整体是一个字符串。如果数据库中字段为varchar类型的,则在SQL语句中写变量时要用‘’括起来,而单引号则在旁边的双引号的外面。数据库中是int型则在SQL语句中不用加。例如:
command.CommandText = "insert into student(Id,Name,Age)values("+Id+",'"+Name+"',"+Age+")";
command.CommandText = "UPDATE student set Name='"+Name+"' ,Age="+Age+" Where Id = "+ Id + "";
command.CommandText = "delete from student where Id=" + Id + "";
command.CommandText = "select Name,Age from student where Id=" + Id + "";
双击DataGridView的一行,实现弹窗,将所选中的值传给下一个窗口的控件中,利用构造函数传值,代码如下:
Form1中:
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
Form3 f3 = new Form3(dataGridView1.CurrentRow); //传过去的是一行,在Form3中接受的是dataGridViewRow的对象
f3.Show();
}
Form3中,代码:
public partial class Form3 : Form
{
public DataGridViewRow dgvr; //传过来的是一行,所以这里声明一个DataGridViewRow
public Form3(DataGridViewRow dgvr1)
{
InitializeComponent();
dgvr = dgvr1;
getValue();
}
public void getValue()
{
textBox1.Text = dgvr.Cells[0].Value.ToString(); //选择行的第一列。
textBox2.Text = dgvr.Cells[1].Value.ToString();
textBox3.Text = dgvr.Cells[2].Value.ToString();
}}
未完待续
252

被折叠的 条评论
为什么被折叠?



