无法从 IIS 使用 DSN 连接到 SQL
Unable to connect to SQL Using DSN from IIS
我正在尝试通过 IIS 连接到我机器中的 DSN。我的代码是这样的
internal class ODBCClass:IDisposable
{
private readonly OdbcConnection oConnection;
private OdbcCommand oCommand;
public ODBCClass(string DataSourceName)
{
//Instantiate the connection
oConnection = new OdbcConnection("Dsn=" + DataSourceName);
try
{
//Open the connection
oConnection.Open();
//Notify the user that the connection is opened
Console.WriteLine("The connection is established with the database");
}
catch (OdbcException caught)
{
Console.WriteLine(caught.Message);
Console.Read();
}
}
public void CloseConnection()
{
oConnection.Close();
}
public OdbcCommand GetCommand(string Query)
{
oCommand = new OdbcCommand();
oCommand.Connection = oConnection;
oCommand.CommandText = Query;
return oCommand;
}
public void Dispose()
{
oConnection.Close();
}
}
public DataSet GetOrderData()
{
using (var o = new ODBCClass("TL"))
{
OdbcCommand oCommand = o.GetCommand("select * from Department");
var oAdapter = new OdbcDataAdapter(oCommand);
var ds = new DataSet();
oAdapter.Fill(ds);
//TO DO : Make use of the data set
return ds;
}
}
当我通过 VisualStudio 运行 作为控制台应用程序时,它可以正常工作。但是当我将其托管到 IIS 并尝试 运行 时,它会抛出这样的错误
System.Data.Odbc.OdbcException: ERROR [28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'WORKGROUP\vg$'.
确保使用系统 DSN,而不是用户 DSN。
也尝试传入用户名和密码。
http://www.carlprothman.net/Technology/ConnectionStrings/ODBCDSN/tabid/89/Default.aspx
您可能还想尝试使用无 DSN 连接。
http://www.carlprothman.net/Technology/ConnectionStrings/ODBCDSNLess/tabid/90/Default.aspx
对于旧的 SQL 服务器版本,您可以使用 "OLE DB Provider for SQL Server"
http://www.carlprothman.net/Default.aspx?tabid=87#OLEDBProviderForSQLServer
对于较新的 SQL 服务器版本,请使用 "Microsoft SQL Server .NET Data Provider"
http://www.carlprothman.net/Default.aspx?tabid=86#SQLClientManagedProvider
HTH
我正在尝试通过 IIS 连接到我机器中的 DSN。我的代码是这样的
internal class ODBCClass:IDisposable
{
private readonly OdbcConnection oConnection;
private OdbcCommand oCommand;
public ODBCClass(string DataSourceName)
{
//Instantiate the connection
oConnection = new OdbcConnection("Dsn=" + DataSourceName);
try
{
//Open the connection
oConnection.Open();
//Notify the user that the connection is opened
Console.WriteLine("The connection is established with the database");
}
catch (OdbcException caught)
{
Console.WriteLine(caught.Message);
Console.Read();
}
}
public void CloseConnection()
{
oConnection.Close();
}
public OdbcCommand GetCommand(string Query)
{
oCommand = new OdbcCommand();
oCommand.Connection = oConnection;
oCommand.CommandText = Query;
return oCommand;
}
public void Dispose()
{
oConnection.Close();
}
}
public DataSet GetOrderData()
{
using (var o = new ODBCClass("TL"))
{
OdbcCommand oCommand = o.GetCommand("select * from Department");
var oAdapter = new OdbcDataAdapter(oCommand);
var ds = new DataSet();
oAdapter.Fill(ds);
//TO DO : Make use of the data set
return ds;
}
}
当我通过 VisualStudio 运行 作为控制台应用程序时,它可以正常工作。但是当我将其托管到 IIS 并尝试 运行 时,它会抛出这样的错误
System.Data.Odbc.OdbcException: ERROR [28000] [Microsoft][SQL Server Native Client 11.0][SQL Server]Login failed for user 'WORKGROUP\vg$'.
确保使用系统 DSN,而不是用户 DSN。 也尝试传入用户名和密码。 http://www.carlprothman.net/Technology/ConnectionStrings/ODBCDSN/tabid/89/Default.aspx
您可能还想尝试使用无 DSN 连接。
http://www.carlprothman.net/Technology/ConnectionStrings/ODBCDSNLess/tabid/90/Default.aspx
对于旧的 SQL 服务器版本,您可以使用 "OLE DB Provider for SQL Server" http://www.carlprothman.net/Default.aspx?tabid=87#OLEDBProviderForSQLServer
对于较新的 SQL 服务器版本,请使用 "Microsoft SQL Server .NET Data Provider" http://www.carlprothman.net/Default.aspx?tabid=86#SQLClientManagedProvider
HTH