尝试连接时出现错误
你调用的对象是空的。
该应用程序正在使用 SQL Server 数据库在我的笔记本电脑上运行,但我需要连接笔记本电脑上的应用程序并Sharkawy-PC
通过网络与我的 PC () 上的 SQL Server 连接我需要app.config
代码和代码帮助以使用来自app.config
.
private void btnlogin_Click(object sender, EventArgs e)
{
SqlCommand cmd;
string UserName = txtusername.Text;
var Connectionstring = ConfigurationManager.ConnectionStrings["BookingConnectionString"].ConnectionString;
SqlConnection conn = new SqlConnection(Connectionstring);
//////SqlConnection conn = new SqlConnection();
//////conn.ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["BookingConnectionString"];
string strsql = "select * from UserInfo,UsersGroup where UserInfo.GroupID=UsersGroup.GroupID and UserName = '" + UserName + "' and UserPassword='" + txtusername.Text + "'";
cmd = new SqlCommand(strsql, conn);
conn.Open();
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
string d = dr.GetString(9).ToString();
if (d == "Admin")
{
AdminMainfrm adminmainfrm = new AdminMainfrm(txtusername.Text);
this.Hide();
adminmainfrm.ShowDialog();
}
else
{
UserMainfrm UserMainfrm = new UserMainfrm();
this.Hide();
UserMainfrm.ShowDialog();
}
}
else
{
label5.Text = "Invalid Username or password, please try again";
}
conn.Close();
}
这是我在 app.config 中的连接
<add name="Booking.Properties.Settings.BookingConnectionString"
connectionString="DRIVER=SQL Server;SERVER=SHARKAWY-PC;UID=sa;PWD=123456;APP=Microsoft® Windows® Operating System;WSID=SHARKAWY;DATABASE=Booking;Network=DBMSSOCN"
providerName="System.Data.SqlClient" />