namespace DataSet对象和DataAdapter操作对象连接数据库
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string conStr = @"Data Source=(localdb)\ProjectsV13;Initial Catalog=123;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
SqlConnection con = new SqlConnection(conStr);
SqlDataAdapter ada = new SqlDataAdapter("select * from cc", con);
DataSet ds = new DataSet();
ada.Fill(ds, "mytable");
dataGridView1.DataSource = ds.Tables["mytable"];
}
}
}
namespace BindingSource组件
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string conStr = @"Data Source=(localdb)\ProjectsV13;Initial Catalog=123;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
SqlConnection con = new SqlConnection(conStr);
SqlDataAdapter ada = new SqlDataAdapter("select * from cc", con);
DataSet ds = new DataSet();
ada.Fill(ds, "table1");
bindingSource1.DataSource = ds.Tables["table1"];
}
private void button1_Click(object sender, EventArgs e)
{
bindingSource1.MovePrevious();
ShowInfo();
}
private void ShowInfo()
{
int index = bindingSource1.Position;
DataRowView DRView = (DataRowView)bindingSource1[index];
textBox1.Text = DRView[1].ToString();
textBox2.Text = DRView[2].ToString();
label4.Text= DRView[0].ToString();
}
private void button2_Click(object sender, EventArgs e)
{
bindingSource1.MoveNext();
ShowInfo();
}
private void button3_Click(object sender, EventArgs e)
{
bindingSource1.MoveFirst();
ShowInfo();
}
private void button4_Click(object sender, EventArgs e)
{
bindingSource1.MoveLast();
ShowInfo();
}
}
}
DataSet插入数据
namespace DataSet增删改操作
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string conStr = @"Data Source=(localdb)\ProjectsV13;Initial Catalog=123;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
SqlConnection con = new SqlConnection(conStr);
SqlDataAdapter ada = new SqlDataAdapter("select * from cc", con);
DataSet ds = new DataSet();
ada.Fill(ds, "mytable");
dataGridView1.DataSource = ds.Tables["mytable"];
}
private void button1_Click(object sender, EventArgs e)
{
string conStr = @"Data Source=(localdb)\ProjectsV13;Initial Catalog=123;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
SqlConnection con = new SqlConnection(conStr);
SqlDataAdapter ada = new SqlDataAdapter("select * from cc", con);
DataSet ds = new DataSet();
ada.Fill(ds, "tables");
dataGridView1.DataSource = ds.Tables["tables"];
//创建SqlCommandBuilder类的对象
SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(ada);
//创建DataRow类的对象
DataRow dr = ds.Tables[0].NewRow();
//设置name列的值
dr["name"] = textBox1.Text;
//设置password列的值
dr["password"] = textBox2.Text;
//向DataTable对象中添加一行
ds.Tables[0].Rows.Add(dr);
//更新数据库
ada.Update(ds.Tables["tables"]);
//刷新dataGridView的数据源
dataGridView1.Refresh();
MessageBox.Show("插入成功!");
}
}
}