c#与SQL

  • 格式:txt
  • 大小:4.12 KB
  • 文档页数:2
textBox4.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
textBox3.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
ds = new DataSet();
sda.Fill(ds,"password");
dt=ds.Tables[0];
dataGridView1.DataSource = dt;
}
mandText = "select * from password where 地址 like '" + textBox3.Text + "%'";
com.Connection = conn;
sda = new SqlDataAdapter(com);
add();
}
private void button2_Click(object sender, EventArgs e)
{
del();
}
private void button4_Click(object sender, EventArgs e)
//dataGridView1.DataSource = dt;
view();
}
public void search()//查询数据
{
SqlCommand com = new SqlCommand();
这样就好了。
SqlCommand cmd = new SqlCommand(bul.ToString(), conn);
cmd.ExecuteNonQuery();
conn.Close();
view();
}
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)//点击dataGridView中的数据时,加载到图上面的textbox中
{
public void edit()//修改数据
{
SqlCommand com = new SqlCommand();
mandText = "update password set 地址='"+textBox3.Text+"',用户名='"+textBox1.Text+"',密码='"+textBox2.Text+"' where 序号='" + textBox4.Text + "'";
com.Connection = conn;
sda = new SqlDataAdapter(com);
ds = new DataSet();
sda.Fill(ds, "password");
//dt = ds.Tables[1];
public void add()//向库中增加数据
{
conn.Open();
StringBuilder bul=new StringBuilder();
bul.Append("insert into password values((select max(序号+1)from password),'" + textBox3.Text + "','" + textBox1.Text + "','" + textBox2.Text + "') ");
search();
}
textBox3.Text = "";
}
private void button1_Click(object sender, EventArgs e)
{
以C#和S Q L数据库工具为例;
实现后台数据库查询,必须先把库建好。
如上图实现的对S Q L 数据库的 增加 删除 查询 修改,原代码如下:
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
DataSet ds;
DataTable dt;
SqlDataAdapter sda;
public Form1()
{
InitializeComponent();
}
public void view()//查看表中数据
{
SqlCommand com = new SqlCommand();
mandText = "select * from password";
com.Connection = conn;
sda = new SqlDataAdapter(com);
public void del()//删除数据
{
SqlCommand com = new SqlCommand();
mandText = "delete password where 序号='"+textBox4.Text+"'";
using System.Data.SqlClient;
using System.Drawing;
using System.Drawing.Drawing2D;
以上为命名空间
public partial class Form1 : Form
{
SqlConnection conn = new SqlConnection(@"data source=forever\maomao;initial catalog=first;integrated security=true");//数据库连接语句
com.Connection = conn;
sda = new SqlDataAdapter(com);
ds = new DataSeassword");
view();
{
if (textBox3.Text == "")
{
view();
}
if (textBox3.Text != "")
{
{
edit();
}
private void button5_Click(object sender, EventArgs e)
{
Application.Exit();
}
ds = new DataSet();
sda.Fill(ds, "password");
dt = ds.Tables[0];
dataGridView1.DataSource=dt;
}
textBox1.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
textBox2.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
view();
}
private void button3_Click(object sender, EventArgs e)//查询按钮

下载文档原格式

  / 2