【发布时间】:2013-12-02 10:28:34
【问题描述】:
我正在用 C# 开发一个类库,在这个类库中我试图通过 ADO.NET 代码访问数据库,但是我收到了这个错误。我不知道这背后的原因是什么。所以请告诉我我该如何解决它。
System.Data.SqlClient.SqlException:尝试为文件 C:\Users\vivek.nuna\Documents\VisualStudio2005\Projects\SubsystemSyncService\TestClient\bin\Debug\aspnetdb.mdf 附加自动命名数据库失败。存在同名数据库,或指定文件无法打开,或位于 UNC 共享上。
这是我正在使用的连接字符串。
<connectionStrings>
<add name="RegistrationConnString"
connectionString="Server=192.168.101.145\SQLEXPRESS;Database=***_HubDB;User Id=sa;Password={C8273EFD-LB2F-4E65-8702-14B61PI08A9}"
providerName="System.Data.SqlClient" />
</connectionStrings>
注意:我在 Debug 文件夹中看不到文件 aspnetdb.mdf。
这是代码,我如何使用 ado.net 代码。
private DataSet GetAddressFieldsAccordingtoAddressId(string strAddressId)
{
try
{
strConnString = ConfigurationManager.ConnectionStrings["RegistrationConnString"].ToString();
SqlConnection connection = new SqlConnection(strConnString);
SqlCommand command = new SqlCommand();
command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandText = "[dbo].[PL_spPLUIGetAddressFieldsAccordingtoAddressId]";
command.Parameters.Add("@lAddressID", System.Data.SqlDbType.Int).Value = Convert.ToInt32(strAddressId);
command.Connection = connection;
DataSet dsPwd = new DataSet();
SqlDataAdapter dAdapter = new SqlDataAdapter(command);
dAdapter.Fill(dsPwd);
command.Dispose();
dAdapter.Dispose();
return dsPwd;
}
catch (Exception ex)
{
return null;
}
}
我以 C# 形式添加了对此类库的引用。
C#窗体调用类库的这个方法。
【问题讨论】:
-
试试这个one
标签: c# asp.net sql-server-2008 ado.net connection-string