连接到数据库c# [英] connect to database c#

查看:171
本文介绍了连接到数据库c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用C#连接到服务器上的DB,但没有运气。



我尝试使用这个:

  public static string m_ConnectionString = 
@Network Library = dbmssocn; Data Source = * server ip *,* port *; database = ;+
@用户id = * db username *;密码= * db pass *;;
public static SqlConnection myConnection = new SqlConnection(m_ConnectionString);

我收到此错误:


连接超时已过期。
尝试使用登录前握手确认时,超时时间已过。这
可能是因为登录前握手失败或服务器
无法及时响应。尝试
连接到此服务器所花费的时间为 - [Pre-Login] initialization = 9343;当我使用 myConnection.Open(); handshake = 5654;​​




$ c>



我也尝试将超时设置为 int.MaxValue / p>

解决方案

SQL Server(和许多其他)连接字符串的一个很好的源是http://www.connectionstrings.com/sql-server/ 。根据是通过ODBC,OLE DB还是Native Client连接,必须选择其他连接字符串。



尝试



Driver = {SQL Native Client}; Server = myServerAddress; = myDataBase;
Uid = myUsername; Pwd = myPassword;



有很多选项可供选择,具体取决于SQL Server版本,安全类型等。






UPDATE b

首先,您必须选择数据访问技术。




  • .NET Framework数据SQL Server提供程序 SqlConnection )是从.NET代码访问SQL Server的首选方法。 (请参阅何时使用SQL Native Client 进行比较)


  • Native Client :以非常快的方式访问SQL Server并支持新功能,因为它直接访问SQL Server TDS协议并适用于非.NET码。


  • ODBC :相对较快且兼容许多不同的数据库。


  • OLEDB :如果数据库类型可能会在将来发生更改,




然后你必须在 SQL之间进行选择服务器身份验证(用户/密码)和 Windows身份验证。如果可能,我会选择后者。使用Windows身份验证,SQL Server假定如果您成功登录到Windows,那么您是一个受信任的用户。然后,Windows用户名将被映射1到1到SQL Server服务器用户。当然,这个用户必须仍然被授予他将在SQL Server上执行的操作所请求的权限(如SELECT,INSERT,UPDATE,DELETE)。如果DBA没有安装Windows身份验证,您必须使用uid / pwd。



这对我有用:

  string connectionString = 
Data Source = 192.168.123.45; Initial Catalog = MyDatabase; Integrated Security = SSPI;;
using(SqlConnection connection = new SqlConnection(connectionString)){
using(SqlCommand command = new SqlCommand(
SELECT Region FROM dbo.tlkpRegion WHERE RegionID = 30,connection)){
connection.Open();
string result =(string)command.ExecuteScalar();
MessageBox.Show(Region =+ result);
}
}


I'm trying to connect to a DB on a server using C# but with no luck.

I tried using this:

public static string m_ConnectionString =
    @"Network Library=dbmssocn; Data Source=*server ip*,*port*; database=*db name*; " +
    @"User id=*db username*; Password=*db pass*;";
public static SqlConnection myConnection = new SqlConnection(m_ConnectionString);

I get this error:

Connection Timeout Expired. The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement. This could be because the pre-login handshake failed or the server was unable to respond back in time. The duration spent while attempting to connect to this server was - [Pre-Login] initialization=9343; handshake=5654;

when I used myConnection.Open();

I tried also to set the timeout to int.MaxValue and it didn't work.

解决方案

A very good source for SQL Server (and many other) connection strings is http://www.connectionstrings.com/sql-server/. Depending whether you are connecting through ODBC, OLE DB or Native Client, you have to choose another connection string.

Try

Driver={SQL Native Client};Server=myServerAddress;Database=myDataBase; Uid=myUsername;Pwd=myPassword;

There are a lot of options to choose from, depending on the exact SQL Server version, the security type and many more.


UPDATE

First you have to choose a data access technology.

  • .NET Framework Data Provider for SQL Server (SqlConnection), Is the preferred way of accessing the SQL Server from .NET code. (See When to use the SQL Native Client for a comparison)

  • Native Client: Is a very fast way of accessing the SQL Server and supports the new features, as it accesses the SQL Server TDS protocol directly and works for non .NET code. It should be preferred for non .NET code.

  • ODBC: Is relatively fast and compatible to a lot of different databases. Choose this one if the data base type might change in future or if you are accessing "exotic" databases.

  • OLEDB: For SQL Server it is relatively slow and will be depreciated by Microsoft.

Then you have to choose between SQL Server Authentication (User/Password) and Windows Authentication. I would choose the latter if possible. With Windows Authentication the SQL-Server assumes that if you logged in successfully to Windows you are a trusted user. The Windows user name will then be mapped 1 to 1 to a SQL-Server user. Of course this user then must still have been granted the rights requested for the operations that he will perform on the SQL Server (like SELECT, INSERT, UPDATE, DELETE). If the DBA didn't install Windows Authentication, you will have to go with uid/pwd.

This worked for me:

string connectionString =
    "Data Source=192.168.123.45;Initial Catalog=MyDatabase;Integrated Security=SSPI;";
using (SqlConnection connection = new SqlConnection(connectionString)) {
    using (SqlCommand command = new SqlCommand(
                 "SELECT Region FROM dbo.tlkpRegion WHERE RegionID=30", connection)) {
        connection.Open();
        string result = (string)command.ExecuteScalar();
        MessageBox.Show("Region = " + result);
    }
}

这篇关于连接到数据库c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆