using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data; using System.Data.SqlClient; namespace WindowsFormsApplication6 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { SqlConnection x = new SqlConnection("Data Source=.;Initial Catalog=XT;Integrated Security=True");//创建连接对象 x.Open();//打开链接 //从表中提取数据 //string str = string.Format("select City from xt"); //DataTable y = new DataTable();//创建一个数据表 //SqlDataAdapter z = new SqlDataAdapter(str, x);//利用str字符串和x数据库连接对象创建适配器 //z.Fill(y);//将适配器内的数据内容填充到数据表中 //插入 // string str = string.Format("insert into xt(ID, LastName, FirstName, Address, City) values (12, '林玲', '林笨笨','2号','商城')"); //string str = string.Format("insert into xt (ID, LastName, City) values (22, '小梅', '忘了')"); //删除 int ID = 3; string str = string.Format("delete from xt where ID = " + ID + ""); SqlCommand n = new SqlCommand(str, x);//创建Command对象,两个参数:需要执行的str,数据连接对象x int i = n.ExecuteNonQuery();//执行命令(多用于执行增加、删除、修改数据,返回受影响的行数) MessageBox.Show("成功!"); } } }