SQL Server远程处理问题 [英] Problem remoting with sql server
问题描述
大家好......
我想将远程处理与sql server一起使用并存储Recordset,但我不知道如何使用它.能否告诉我使用它的方法...谢谢高级……. .
Hi All....
I want to use remoting with sql server and store Recordset,but I don''t know how to use it.Could you tell me about the way to use it... Thank in advanced...........
推荐答案
我对C#不熟悉,但是知道如何在VB.Net
中做到这一点.
我已经翻译了我的代码;
I am not familiar with C#, but know how to do this in VB.Net
I have translated my code;
using System.Data.Sql;
using System.Data.SqlClient;
public frmMain()
{
InitializeComponent();
}
private void frmMain_Load(object sender, EventArgs e)
{
SqlConnection ExternalSQLConnection = new SqlConnection();
ExternalSQLConnection.ConnectionString = "Data Source=YourServerName\\YourSQLInstanceName; Initial Catalog = YourDatabase; UID=YourUserName;PWD=YourPassword"
//eg: "Data Source=HEATDB1CW; Initial Catalog=Northwind; User Id=heat; Password=h34t"
这将打开与外部数据库的连接.
您还可以使用SqlConnectionStringBuilder生成连接字符串
This will open a connection to the external database.
You could also use SqlConnectionStringBuilder to build the connection string
SqlConnectionStringBuilder bu = new SqlConnectionStringBuilder();
bu.DataSource = @"YourServer\YourInstance";
bu.InitialCatalog = "YourDatabase" ;
bu.IntegratedSecurity = false ; // Sql Server Authentification
bu.UserId = "YourSqlLogin";
bu.Password = "YourPassword";
ExternalSQLConnection.ConnectionString = bu.ConnectionString;
然后,您需要配置SQL Server以接受远程连接.可以使用SQL Server>配置工具> SQL Server外围应用配置器.选择"TCP/IP的本地和远程连接".
请参阅以下支持页面以了解操作方法.
http://support.microsoft.com/default.aspx/kb/914277
不同版本的SQL Server的说明可能有所不同.
You then need to configure SQL server to accept remote connection. This can be done using SQL Server > Configuration Tools > SQL Server Surface Area Configuration. Select Local and Remote connections for TCP/IP.
Please see following support page on how to do it.
http://support.microsoft.com/default.aspx/kb/914277
Instructions may differ between versions of SQL Server.
现在,您可以打开连接并使用SqlDataReader读取信息.对于此示例,我将使用来自SQL查询的信息填充数据表.
You can now open the connection and read the information using the SqlDataReader. For this example I will populate a datatable with the information from the SQL Query.
//
DataTable dt = new DataTable();
SqlCommand selectCommandString = "Sql command you wish to run"
SqlDataAdapter sda = new SqlDataAdapter(selectCommandString,
bu.ConnectionString);
sda.Fill(dt);
//
除此之外,您可以尝试使用oledb命名空间.
Additional to this, you could try using the oledb namespace.
using System.Data;
using System.Data.OleDb;
// Refer to the OleDbConnection namespace for correct syntax of connection string
string connStr = "Your connection string here"
OleDbConnection con=new OleDbConnection(connStr);
con.Open();
OleDbDataReader rs=new OleDbDataReader();
OleDbCommand cmd=new OleDbCommand();
cmd.Connection=con;
cmd.CommandText="select * from table1";
cmd.CommandType=CommandType.Text;
rs=cmd.ExecuteReader(CommandBehavior.CloseConnection);
while(rs.Read())
{
string data1=rs["name"].ToString();
}
我希望这有帮助!尽管下一次更多有关您要实现的目标的详细信息(IE:确切的查询,您的当前代码等)将很有帮助!
I hope this helps! Although next time a little more information on what exactly you are trying to achieve (IE: The exact query, your current code etc) would have been helpful!
这篇关于SQL Server远程处理问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!