gpt4 book ai didi

javascript - 从经典 ASP 应用程序连接到 SQLExpress

转载 作者:行者123 更新时间:2023-12-02 15:34:57 26 4
gpt4 key购买 nike

我有一个本地 SQLExpress 数据库。我需要从经典 ASP Web 应用程序连接到此。

我认为我的连接字符串是正确的,因为当我使用 C# 控制台应用程序进行测试时,我能够连接并读取数据:

// C# Example app
var connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=TestData;Integrated Security=True";
var con = new SqlConnection(connectionString);

con.Open();
var cmd = new SqlCommand("SELECT * FROM tbl", con);

cmd.ExecuteNonQuery();
var reader = cmd.ExecuteReader();
while(reader.Read())
{
Console.WriteLine(reader.GetValue(1)); // displays data
}

但是,当我在 ASP Web 应用程序中使用相同的字符串时,它会失败:

// ASP Web App
connection = Server.CreateObject("ADODB.Connection");
connectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=TestData;Integrated Security=True";
Response.Write("connectionString:" + connectionString + "<br/>");
connection.Open(Application(connectionString)); // error

Open 方法失败并显示:

0x80004005 - Microsoft OLE DB Provider for ODBC Drivers: [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified

我还尝试为 SQL 数据库设置登录名并使用用户 ID 和密码:

var connectionString = @"Data Source=.\SQLEXPRESS;Initial Catalog=TestData;User Id=AspTest;Password=Test";

这也会以同样的方式失败。

如何让我的网络应用程序连接到我的数据库?

最佳答案

如果您显示的 ASP 代码是您实际使用的代码,则 Application(connectionString) 很可能返回 null/nothing,因为应用程序对象是字典。您是否尝试过直接使用连接字符串打开连接:

connection.Open(connectionString); 

通常的模式是在 global.asa 中分配 Application("conectionString") = "your connection string",然后使用以下代码创建连接:

connection.Open(Application("connectionString")); 

此外,正如前面的答案所示,您将需要 ASP 和 .NET 的不同连接字符串。

关于javascript - 从经典 ASP 应用程序连接到 SQLExpress,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33020392/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com