我在调试程序时遇到错误,它是一个带有asp.net c#的Web服务登录和用户跟踪系统。以前是sql异常错误,我已经通过更改连接字符串来修复它并且它更改为另一个错误,下面是我的代码:
[WebMethod]
public string Login(string Username, string Password)
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True;");
con.Open();
SqlCommand cmd = new SqlCommand("SELECT Username, Password from UserData where Username = @Username AND Password = @Password ", con);
cmd.Parameters.AddWithValue("@UserName", Username);
cmd.Parameters.AddWithValue("@Password", Password);
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
// Create an instance of DataSet.
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
string result = "Successful";
int count = ds.Tables[0].Rows.Count;
if (count == 1)
{
SqlConnection con1 = new SqlConnection(@"Data Source=.\SQLEXPRESS;Integrated Security=True;User Instance=True;");
con1.Open();
DateTime dt = DateTime.Now;
SqlCommand cmd1 = new SqlCommand("INSERT INTO ActivityLog (CreateOn, CreateBy, UpdateOn, UpdateBy) VALUES (@CreateOn,@CreateBy, @UpdateOn, @UpdateBy ", con1);
cmd1.Parameters.AddWithValue("@CreateOn", dt);
cmd1.Parameters.AddWithValue("@CreateBy", Username);
cmd1.Parameters.AddWithValue("@UpdateOn", dt);
cmd1.Parameters.AddWithValue("@UpdateBy", Username);
cmd1.ExecuteNonQuery();
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
// Create an instance of DataSet.
DataSet ds1 = new DataSet();
da1.Fill(ds);
con1.Close();
return result;
}
else
{
result = "Fail";
return result;
}
}
此处显示错误:
System.Data.SqlClient.SqlException: Invalid object name 'UserData'.
我不确定是连接字符串错误还是我的代码不正确?
答案 0 :(得分:0)
看看这个:
connectionString="Data Source=192.168.68.42\SQL2017STD;Initial Catalog=YourDBNameHere;User ID=sampleusername;Password=yourpassword; connection timeout=6000" providerName="System.Data.SqlClient"