急!急!急!求一个ADO.NET 程序要求连接数据库。实现增加,修改,删除,查询功能。 20
我有标准希望大大们给修改一下。改好了我追加分的哦usingSystem;usingSystem.Collections.Generic;usingSystem.Compo...
我有标准 希望大大们给修改一下。改好了 我追加分的哦
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 查询
{
public partial class Form1 : Form
{
SqlConnection cn;
SqlDataAdapter da;
DataSet ds;
public Form1()
{
InitializeComponent();
cn = new SqlConnection();
cn.ConnectionString = "data source= 张艾辰-THINK;initial catalog=master;integrated security=sspi;";
cn.Open();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection sc = new SqlConnection();
sc.ConnectionString = "data source= 张艾辰-THINK;initial catalog=master;integrated security=sspi;";
sc.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = sc;
cmd.CommandType = CommandType.Text;
//cmd.CommandText = "select * from 员工";
cmd.CommandText = "select id from 员工;" + "select name from 员工";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
//listBox1.Items.Add(dr.GetInt32(0) + " " + dr.GetString(1));
listBox1.Items.Add(dr.GetInt32(0));
}
dr.NextResult();
while (dr.Read())
listBox2.Items.Add(dr.GetString(0));
dr.Close();
sc.Dispose();
sc.Close();
}
private void button2_Click(object sender, EventArgs e)
{
da = new SqlDataAdapter("select * from s103061", cn);
ds = new DataSet();
da.Fill(ds, "student");
dataGridView1.DataSource = ds.Tables["student"];
}
private void button3_Click(object sender, EventArgs e)
{
SqlCommandBuilder cb = new SqlCommandBuilder(da);
da.Update(ds, "student");
MessageBox.Show("更新成功");
}
private void button4_Click(object sender, EventArgs e)
{
SqlCommandBuilder cb = new SqlCommandBuilder(da);
MessageBox.Show("删除成功");
}
}
} 展开
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace 查询
{
public partial class Form1 : Form
{
SqlConnection cn;
SqlDataAdapter da;
DataSet ds;
public Form1()
{
InitializeComponent();
cn = new SqlConnection();
cn.ConnectionString = "data source= 张艾辰-THINK;initial catalog=master;integrated security=sspi;";
cn.Open();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection sc = new SqlConnection();
sc.ConnectionString = "data source= 张艾辰-THINK;initial catalog=master;integrated security=sspi;";
sc.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = sc;
cmd.CommandType = CommandType.Text;
//cmd.CommandText = "select * from 员工";
cmd.CommandText = "select id from 员工;" + "select name from 员工";
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
//listBox1.Items.Add(dr.GetInt32(0) + " " + dr.GetString(1));
listBox1.Items.Add(dr.GetInt32(0));
}
dr.NextResult();
while (dr.Read())
listBox2.Items.Add(dr.GetString(0));
dr.Close();
sc.Dispose();
sc.Close();
}
private void button2_Click(object sender, EventArgs e)
{
da = new SqlDataAdapter("select * from s103061", cn);
ds = new DataSet();
da.Fill(ds, "student");
dataGridView1.DataSource = ds.Tables["student"];
}
private void button3_Click(object sender, EventArgs e)
{
SqlCommandBuilder cb = new SqlCommandBuilder(da);
da.Update(ds, "student");
MessageBox.Show("更新成功");
}
private void button4_Click(object sender, EventArgs e)
{
SqlCommandBuilder cb = new SqlCommandBuilder(da);
MessageBox.Show("删除成功");
}
}
} 展开
3个回答
展开全部
namespace Ch16_Exam3_2
{
public partial class MainPage : UserControl
{
FridendContext fridendcontext;
public MainPage()
{
InitializeComponent();
fridendcontext= new FridendContext();
LoadData();
}
void LoadData()
{
dataGrid1.ItemsSource = fridendcontext.Friends;
fridendcontext.Load(FridendContext.GetFriendQuery());
}
//增加记录
private void btAdd_Click(object sender, RoutedEventArgs e)
{
Friend fid = new Friend();
fid.Name = "张刚"+System .DateTime .Now .Millisecond.ToString ();
fid.Address = "内蒙古市";
fid.Phone = "87663560";
fridendcontext.Friends.Add(fid);
fridendcontext.SubmitChanges();
MessageBox.Show("增加成功");
}
//删除记录
private void btDele_Click(object sender, RoutedEventArgs e)
{
Friend fid = (Friend)dataGrid1.SelectedItem;
fridendcontext.Friends.Remove(fid);
fridendcontext.SubmitChanges();
MessageBox.Show("删除成功");
}
//修改记录
private void btAdapt_Click(object sender, RoutedEventArgs e)
{
Friend fid = (Friend)dataGrid1.SelectedItem;
fid.Address = "北京市南京路";
fridendcontext.SubmitChanges();
MessageBox.Show("修改成功");
}
}
}
{
public partial class MainPage : UserControl
{
FridendContext fridendcontext;
public MainPage()
{
InitializeComponent();
fridendcontext= new FridendContext();
LoadData();
}
void LoadData()
{
dataGrid1.ItemsSource = fridendcontext.Friends;
fridendcontext.Load(FridendContext.GetFriendQuery());
}
//增加记录
private void btAdd_Click(object sender, RoutedEventArgs e)
{
Friend fid = new Friend();
fid.Name = "张刚"+System .DateTime .Now .Millisecond.ToString ();
fid.Address = "内蒙古市";
fid.Phone = "87663560";
fridendcontext.Friends.Add(fid);
fridendcontext.SubmitChanges();
MessageBox.Show("增加成功");
}
//删除记录
private void btDele_Click(object sender, RoutedEventArgs e)
{
Friend fid = (Friend)dataGrid1.SelectedItem;
fridendcontext.Friends.Remove(fid);
fridendcontext.SubmitChanges();
MessageBox.Show("删除成功");
}
//修改记录
private void btAdapt_Click(object sender, RoutedEventArgs e)
{
Friend fid = (Friend)dataGrid1.SelectedItem;
fid.Address = "北京市南京路";
fridendcontext.SubmitChanges();
MessageBox.Show("修改成功");
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询