用c#如何在服务器和客户机之间传递在数据库中查询到的表? 我的数据库存放在服务器上,在客户端把查询语句传送到服务器上,服务器在数据库中查询后得到一个表,请问怎样才能把这个表传送到客户端并在datagrid中显示?哪位高手帮帮我啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在服务器端执行SQL语句获取数据WEB中可直接显示在页面 服务器开放1433端口,设置TCP/IP协议为允许,选择混合验证模式,添加一个账户,设置密码。客户端连接字符串直接指向服务器即可。 第一种方法:用web服务第二种方法:服务器端将数据取到表后序列化,客户端收到后再反序列化第三种方法:DataTable.WriteXml写入流或文件,客户端再dataTable.ReadXml() Socket自己写一个通讯的! 人家说的是datagridview,什么web啊,分我拿了using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Net;using System.Net.Sockets;using System.Data.SqlClient;using System.Threading;namespace sqlserver{ delegate void myeverthandler(); public partial class Form1 : Form { byte[] data = new byte[1024]; public Form1() { InitializeComponent(); Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 8000); s.Bind(ipep); s.Listen(10); s.BeginAccept(new AsyncCallback(Accept), s); //label1.Text = data.Length.ToString(); } void Accept(IAsyncResult iar) { Socket s = (Socket)iar.AsyncState; Socket server = s.EndAccept(iar); label1.Invoke(new myeverthandler(LabelProc2)); server.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(WaitForData), server); } void WaitForData(IAsyncResult iar) { Socket s = (Socket)iar.AsyncState; int recv = s.EndReceive(iar); //if (recv == 0) //{ //label1.Invoke(new myeverthandler(LabelProc1)); //重新beginaccept(); //.... //.... //} //MessageBox.Show(recv.ToString());//1024 //label1.Text = data.Length.ToString(); DataProcess(data); //try //{ //byte[] data = Encoding.ASCII.GetBytes("haha"); //s.Send(data); //} //catch (SocketException) //{ //label1.Invoke(new myeverthandler(LabelProc1)); //} s.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(WaitForData), s); } public void DataProcess(byte[] data) { SqlConnection sc = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True"); SqlDataAdapter sda = new SqlDataAdapter("select *from shit", sc); DataSet ds = new DataSet(); sda.Fill(ds, "temp"); SqlCommandBuilder scbld = new SqlCommandBuilder(sda); DataRow dr = ds.Tables["temp"].NewRow(); int place = 0; int namelength = BitConverter.ToInt32(data, place); place += 4; dr["name"] = Encoding.Unicode.GetString(data, place, namelength); place += namelength; int age = BitConverter.ToInt32(data, place); dr["age"] = age; place += 4; int citylength = BitConverter.ToInt32(data, place); place += 4; dr["city"] = Encoding.Unicode.GetString(data, place, citylength); place += citylength; dr["zip"] = Encoding.ASCII.GetString(data, place, 6); ds.Tables["temp"].Rows.Add(dr); sda.Update(ds, "temp"); //sda.Update(dr); this.shitTableAdapter.Fill(this.masterDataSet.shit); shitBindingSource.DataSource = masterDataSet; dataGridView1.DataSource = shitBindingSource; //dataGridView1.Refresh(); } void LabelProc1() { label1.Text = "disconneted with client"; } void LabelProc2() { label1.Text = "connected with client"; } private void Form1_Load(object sender, EventArgs e) { // TODO: 这行代码将数据加载到表“masterDataSet.shit”中。您可以根据需要移动或移除它。 this.shitTableAdapter.Fill(this.masterDataSet.shit); } private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) { } private void label1_Click(object sender, EventArgs e) { } }} 这是把数据库内容转换为byte的函数 /* public byte[] GetBytes() { byte[] data = new byte[1024]; int place = 0; //Buffer.BlockCopy(BitConverter.GetBytes(textBox1.Text.Length), 0, data, place, 4); //place += 4; //Buffer.BlockCopy(Encoding.ASCII.GetBytes(textBox1.Text), 0, data, place, textBox1.Text.Length); //place += textBox1.Text.Length; //name... byte[] bs = Encoding.Unicode.GetBytes(textBox1.Text); Buffer.BlockCopy(BitConverter.GetBytes(bs.Length), 0, data, place, 4); place += 4; Buffer.BlockCopy(bs, 0, data, place, bs.Length); place += bs.Length; //age... int age = Convert.ToInt32(textBox2.Text); Buffer.BlockCopy(BitConverter.GetBytes(age), 0, data, place, 4); place += 4; //city... byte[] bs1 = Encoding.Unicode.GetBytes(textBox3.Text); Buffer.BlockCopy(BitConverter.GetBytes(bs1.Length), 0, data, place, 4); place += 4; Buffer.BlockCopy(bs1, 0, data, place, bs1.Length); place += bs1.Length; //zip... Buffer.BlockCopy(Encoding.ASCII.GetBytes(textBox4.Text), 0, data, place, textBox4.Text.Length); place += 6; return data; } string sqlcon = "data source=sql服务器名\\sql服务器名;database=流量计管理系统;user id=sa;pwd=";SqlConnection mycon = new SqlConnection(sqlcon);mycon.Open();vs通过上面语句建立与数据库服务器的通信。要注意打开Sql Server2005 的远程连接,其设置在“配置工具”的"sql server外围应用配置器"下。就可以像访问本地一样访问数据库。 用 dataset 来查询,数据库会自动返回到 dataset,然后你在里面自己读取就行了 c# 算法 请问如何将DataGridViewTextBoxColumn和DataGridViewButtonColumn组合成一个新列,即带编辑按钮的列? 如何播放CD? C#中的配置 COM 组件免注册激活问题 一个你应该曾经遇到过的问题?菜了求救!——————在线等,甚盼 点击DataGrid的单元格,单元格里面就变成文本框,修改里面的数据,点击别的单元格里面的刚才的数据被修改 .net中如何实现数字签名 C#MOBILE开发一个简单的显示图片页面却报错~! 如何把picture绘制的图像存 请教??? Rabbit繁殖 C# webbrowser 浏览网页时,给文本框赋值?如何写?
WEB中可直接显示在页面
第二种方法:服务器端将数据取到表后序列化,客户端收到后再反序列化
第三种方法:DataTable.WriteXml写入流或文件,客户端再dataTable.ReadXml()
自己写一个通讯的!
什么web啊,分我拿了using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Data.SqlClient;
using System.Threading;namespace sqlserver
{
delegate void myeverthandler();
public partial class Form1 : Form
{
byte[] data = new byte[1024];
public Form1()
{
InitializeComponent();
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPEndPoint ipep = new IPEndPoint(IPAddress.Any, 8000);
s.Bind(ipep);
s.Listen(10);
s.BeginAccept(new AsyncCallback(Accept), s);
//label1.Text = data.Length.ToString(); }
void Accept(IAsyncResult iar)
{
Socket s = (Socket)iar.AsyncState;
Socket server = s.EndAccept(iar);
label1.Invoke(new myeverthandler(LabelProc2));
server.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(WaitForData), server);
}
void WaitForData(IAsyncResult iar)
{
Socket s = (Socket)iar.AsyncState;
int recv = s.EndReceive(iar);
//if (recv == 0)
//{
//label1.Invoke(new myeverthandler(LabelProc1));
//重新beginaccept();
//....
//....
//}
//MessageBox.Show(recv.ToString());//1024
//label1.Text = data.Length.ToString();
DataProcess(data);
//try
//{
//byte[] data = Encoding.ASCII.GetBytes("haha");
//s.Send(data);
//}
//catch (SocketException)
//{
//label1.Invoke(new myeverthandler(LabelProc1));
//}
s.BeginReceive(data, 0, data.Length, SocketFlags.None, new AsyncCallback(WaitForData), s);
}
public void DataProcess(byte[] data)
{
SqlConnection sc = new SqlConnection("Data Source=.;Initial Catalog=master;Integrated Security=True");
SqlDataAdapter sda = new SqlDataAdapter("select *from shit", sc);
DataSet ds = new DataSet();
sda.Fill(ds, "temp");
SqlCommandBuilder scbld = new SqlCommandBuilder(sda);
DataRow dr = ds.Tables["temp"].NewRow();
int place = 0;
int namelength = BitConverter.ToInt32(data, place);
place += 4;
dr["name"] = Encoding.Unicode.GetString(data, place, namelength);
place += namelength;
int age = BitConverter.ToInt32(data, place);
dr["age"] = age;
place += 4;
int citylength = BitConverter.ToInt32(data, place);
place += 4;
dr["city"] = Encoding.Unicode.GetString(data, place, citylength);
place += citylength;
dr["zip"] = Encoding.ASCII.GetString(data, place, 6); ds.Tables["temp"].Rows.Add(dr);
sda.Update(ds, "temp");
//sda.Update(dr);
this.shitTableAdapter.Fill(this.masterDataSet.shit);
shitBindingSource.DataSource = masterDataSet;
dataGridView1.DataSource = shitBindingSource;
//dataGridView1.Refresh();
}
void LabelProc1()
{
label1.Text = "disconneted with client";
} void LabelProc2()
{
label1.Text = "connected with client";
} private void Form1_Load(object sender, EventArgs e)
{
// TODO: 这行代码将数据加载到表“masterDataSet.shit”中。您可以根据需要移动或移除它。
this.shitTableAdapter.Fill(this.masterDataSet.shit);
} private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{ } private void label1_Click(object sender, EventArgs e)
{ }
}
}
/* public byte[] GetBytes()
{
byte[] data = new byte[1024];
int place = 0;
//Buffer.BlockCopy(BitConverter.GetBytes(textBox1.Text.Length), 0, data, place, 4);
//place += 4;
//Buffer.BlockCopy(Encoding.ASCII.GetBytes(textBox1.Text), 0, data, place, textBox1.Text.Length);
//place += textBox1.Text.Length;
//name...
byte[] bs = Encoding.Unicode.GetBytes(textBox1.Text);
Buffer.BlockCopy(BitConverter.GetBytes(bs.Length), 0, data, place, 4);
place += 4;
Buffer.BlockCopy(bs, 0, data, place, bs.Length);
place += bs.Length;
//age...
int age = Convert.ToInt32(textBox2.Text);
Buffer.BlockCopy(BitConverter.GetBytes(age), 0, data, place, 4);
place += 4;
//city...
byte[] bs1 = Encoding.Unicode.GetBytes(textBox3.Text);
Buffer.BlockCopy(BitConverter.GetBytes(bs1.Length), 0, data, place, 4);
place += 4;
Buffer.BlockCopy(bs1, 0, data, place, bs1.Length);
place += bs1.Length;
//zip...
Buffer.BlockCopy(Encoding.ASCII.GetBytes(textBox4.Text), 0, data, place, textBox4.Text.Length);
place += 6;
return data;
}
SqlConnection mycon = new SqlConnection(sqlcon);
mycon.Open();
vs通过上面语句建立与数据库服务器的通信。要注意打开Sql Server2005 的远程连接,其设置在“配置工具”的"sql server外围应用配置器"下。就可以像访问本地一样访问数据库。