private NewsData data; private void button3_Click(object sender, EventArgs e) //发送 { string x = textBox1.Text; //将输入框的内容存到x里面 new NewsDA().Insert(x);//然后将x存到数据库里面去 textBox1.Text = "";//清空一下原来的输入框 data = new NewsDA().Select1();//调用一个查询方法 ,查一下刚刚存到数据库里的数据 if(data!=null) { textBox2.Text = textBox2.Text + " " + data.Neirong;//将查到的刚存到数据库的数据放到右边的文本框里 } } private List<NewsData> sss; private string y; private void timer1_Tick(object sender, EventArgs e) //定时执行事件 { sss = new NewsDA().Select2(); //查一下对方发过来的信息(也是先存到数据库,但是是另外一个表里),然后定时查询 //if(sss!=null) //定时查询后每次都存到左边的文本框里 //{ // textBox3.Text = sss[i].Neirong; //} if (sss != null) { for (int i = 0; i < sss.Count; i++) { y = sss[i].Neirong; //查出来的内容 } } textBox3.Text = y; }
---------------------------------另一个对话框的代码一样,只是查询的各自的表相反
private NewsData data; private void button1_Click(object sender, EventArgs e) //发送 { string x = textBox1.Text; //将输入框的文本存到x里面 new NewsDA().Insertagain(x);//将x存到数据库里面 textBox1.Text = ""; data = new NewsDA().Select4(); if (data != null) { textBox2.Text = textBox2.Text + " " + data.Neirong; } } private void timer1_Tick(object sender, EventArgs e) //定时执行事件 { string zqqq = ""; List<NewsData> s =new NewsDA().Select5();//查一下自己发过去的消息 if(s!=null) { for (int i = 0; i < s.Count; i++) { zqqq = s[i].Neirong;//查到之后显示在左边的文本框里 } textBox3.Text = zqqq; } } }
-------------下面是NewsDA 大部分都一样,写的很乱。其实就几个,有一些是没用的
public class NewsDA { private SqlConnection Conn; private SqlCommand Cmd; public NewsDA() { Conn = new SqlConnection("server=.;database=Tencent;user=sa;pwd=123"); Cmd = Conn.CreateCommand(); } public void Insert(string x) { Conn.Open(); Cmd.CommandText = "insert into news values(@z)"; Cmd.Parameters.Clear(); Cmd.Parameters.Add("@z",x); Cmd.ExecuteNonQuery(); Cmd.Dispose(); Conn.Close(); } public void Insertagain(string x) { Conn.Open(); Cmd.CommandText = "insert into newsagain values(@z)"; Cmd.Parameters.Clear(); Cmd.Parameters.Add("@z", x); Cmd.ExecuteNonQuery(); Cmd.Dispose(); Conn.Close(); } public List<NewsData> Select() { List<NewsData> list = null; Conn.Open(); Cmd.CommandText = "select * from news"; SqlDataReader dr = Cmd.ExecuteReader(); if (dr.HasRows) { list = new List<NewsData>(); while (dr.Read()) { NewsData s = new NewsData(); s.Neirong = dr["neirong"].ToString(); list.Add(s); } } return list; } public NewsData Select1() { NewsData list = null; Conn.Open(); Cmd.CommandText = "select * from news"; SqlDataReader dr = Cmd.ExecuteReader(); if (dr.HasRows) { list = new NewsData(); while (dr.Read()) { list.Neirong = dr["neirong"].ToString(); } } return list; } public List<NewsData> Select2() { List<NewsData> list = null; Conn.Open(); Cmd.CommandText = "select* from newsagain"; SqlDataReader dr = Cmd.ExecuteReader(); if (dr.HasRows) { list = new List<NewsData>(); while (dr.Read()) { NewsData a = new NewsData(); a.Neirong = dr["neirong"].ToString(); list.Add(a); } } return list; } public NewsData Select3() { NewsData list = null; Conn.Open(); Cmd.CommandText = "select top 1* from newsagain order by ids desc"; SqlDataReader dr = Cmd.ExecuteReader(); if (dr.HasRows) { list = new NewsData(); while (dr.Read()) { list.Neirong = dr["neirong"].ToString(); } } return list; } public NewsData Select4() { NewsData list = null; Conn.Open(); Cmd.CommandText = "select * from newsagain "; SqlDataReader dr = Cmd.ExecuteReader(); if (dr.HasRows) { list = new NewsData(); while (dr.Read()) { list.Neirong = dr["neirong"].ToString(); } } return list; } public List<NewsData> Select5() { List<NewsData> list = null; Conn.Open(); Cmd.CommandText = "select* from news"; SqlDataReader dr = Cmd.ExecuteReader(); if (dr.HasRows) { list = new List<NewsData>(); while (dr.Read()) { NewsData a = new NewsData(); a.Neirong = dr["neirong"].ToString(); list.Add(a); } } return list; } public void Delete1(string ids) { Conn.Open(); Cmd.CommandText = "delete from news where ids='"+ids+"'"; Cmd.ExecuteReader(); Cmd.Dispose(); Conn.Close(); } public void Deleteagain() { Conn.Open(); Cmd.CommandText = "delete from newsagain "; Cmd.ExecuteReader(); Cmd.Dispose(); Conn.Close(); } public void Delete2(string ids) { Conn.Open(); Cmd.CommandText = "delete from newsagain where ids='" + ids + "'"; Cmd.ExecuteReader(); Cmd.Dispose(); Conn.Close(); } public void Delete() { Conn.Open(); Cmd.CommandText = "delete from news"; Cmd.ExecuteReader(); Cmd.Dispose(); Conn.Close(); } //第二个 public NewsData Selectagain() { NewsData list = null; Conn.Open(); Cmd.CommandText = "select * from newsagain"; SqlDataReader dr = Cmd.ExecuteReader(); if (dr.HasRows) { list = new NewsData(); while (dr.Read()) { list.Neirong = dr["neirong"].ToString(); } } return list; }