【问题标题】:Connecto to SQL Server with c# and JDBC使用 c# 和 JDBC 连接到 SQL Server
【发布时间】:2015-09-27 00:20:12
【问题描述】:

我在java 中有这个程序可以连接到SQL Server

server = "ZF-SQL-MTRAZDB.NIS.LOCAL"
dbName = "MRAZ"
nameBaseDatos = "CD_LO"
table = "dbo.CD_LO_DATA"
user = "user"
password = "Pass"
url = "jdbc:sqlserver//"+ server + "\\" + dbName + "jdatabaseName=" +     nameBaseDatos
driver = "com.microsoft.sqlserver.jdbc_SQLServerDriver"

现在我必须对Windows XP 中的Visual C# 2010 做同样的事情

我该如何做这个程序?因为在java中使用JDBC,我应该也使用JDBC吗?

谢谢大家!

【问题讨论】:

  • 使用SqlConnection连接SQL Server,看看这个页面:msdn.microsoft.com/en-us/library/…
  • 我使用这个:SqlCommand command = new SqlCommand(queryString, connection); command.Connection.Open(); command.ExecuteNonQuery(); 但queryString 是什么?和connection? URL?? server??

标签: java c# jdbc visual-c#-express-2010 windows-xp-sp3


【解决方案1】:

ConnectionString 类似于OLE DB 连接字符串,但不相同。与 OLE DB 或 ADO 不同,返回的连接字符串与用户设置的 ConnectionString 相同,如果 Persist Security Info 值设置为 false(默认),则减去安全信息。 SQL Server 的 .NET Framework 数据提供程序不会在连接字符串中保留或返回密码,除非您将 Persist Security Info 设置为 true。

您可以使用ConnectionString 属性连接到数据库。以下示例说明了一个典型的连接字符串。

"Persist Security Info=False;Integrated Security=true;Initial Catalog=Northwind;server=(local)"

使用新的SqlConnectionStringBuilder 在运行时构造有效的连接字符串。

private static void OpenSqlConnection()
{
    string connectionString = GetConnectionString();

    using (SqlConnection connection = new SqlConnection())
    {
        connection.ConnectionString = connectionString;

        connection.Open();

        Console.WriteLine("State: {0}", connection.State);
        Console.WriteLine("ConnectionString: {0}",
            connection.ConnectionString);
    }
}

static private string GetConnectionString()
{
    // To avoid storing the connection string in your code,  
    // you can retrieve it from a configuration file. 
    return "Data Source=MSSQL1;Initial Catalog=AdventureWorks;"
        + "Integrated Security=true;";
}
  1. Data Source 或 Server orAddressorAddrorNetwork Address : The name or network address of the instance of SQL Server to which to connect. The port number can be specified after the server name : server=tcp:servername, portnumber`
  2. Initial Catalog 或 Database :数据库的名称。数据库名称不得超过 128 个字符。
  3. Integrated Security 或 Trusted_Connection :当在连接中指定 false、User ID 和 Password 时。如果为 true,则使用当前 Windows 帐户凭据进行身份验证。识别值为真、false、是、否和sspi(强烈推荐),相当于true。如果指定了User ID 和Password 并将Integrated Security 设置为true,则User ID 和Password 将被忽略并使用Integrated Security。

和other items

我希望这对你有帮助:)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 2014-12-01
    • 2012-12-04
    • 2014-01-02
    • 2014-03-03
    • 1970-01-01
    • 2013-04-13
    相关资源
    最近更新 更多