如何直接连接到aspnetdb数据库

时间:2010-10-19 08:21:37

标签: asp.net sql database asp.net-membership database-connection

我想连接到aspnetdb但是它出错了“用户登录失败” 这是web config中的连接字符串:

<add name="UserProfiles" connectionString="Data Source=KIA;Initial Catalog=aspnetdb;Integrated Security=True;"
   providerName="System.Data.SqlClient" />

这是我的代码:

SqlConnection connection = new SqlConnection();
        SqlCommand ComNewCheckSum = new SqlCommand();
        connection.ConnectionString = ConfigurationManager.ConnectionStrings["UserProfiles"].ConnectionString;
        connection.Open();
ComNewCheckSum.Connection = connection;
            ComNewCheckSum.CommandText = String.Format("select UserID from aspnet_Users where UserName = {0}", _UserName);

            return Convert.ToInt32(ComNewCheckSum.ExecuteScalar());

我怎么能通过错误?感谢

1 个答案:

答案 0 :(得分:1)

在连接字符串中,您使用的是“Integrated Security = True”,这意味着您尝试使用Windows凭据连接到此数据库,这似乎不利于访问此数据库。

尝试使用您知道可以访问此数据库的用户和密码,并更新您的连接字符串,以便它使用该信息 - 例如:

<add name="UserProfiles" connectionString="Data Source=KIA;Initial Catalog=aspnetdb;User ID=someuser;Password=somepassword;"
   providerName="System.Data.SqlClient" />
相关问题