1

尝试连接时出现错误

你调用的对象是空的。

该应用程序正在使用 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" />
4

2 回答 2

1

您需要做的一件事是更改此行:

var Connectionstring = ConfigurationManager.ConnectionStrings["BookingConnectionString"].ConnectionString;

到:

var Connectionstring = ConfigurationManager.ConnectionStrings["Booking.Properties.Settings.BookingConnectionString"].ConnectionString; 

那应该可以解决对象引用错误。


我要推荐的下一件事是您简化连接字符串,您不需要所有这些信息,只需使用以下内容:

<add name="Booking.Properties.Settings.BookingConnectionString"
     connectionString="SERVER=SHARKAWY-PC;UID=sa;PWD=123456;DATABASE=Booking"
     providerName="System.Data.SqlClient" />

接下来我要推荐的是,即使在测试期间,您也不要使用该sa帐户,如果您习惯了它,您将采用这种方式进行部署。

我要推荐的最后一件事是您使用参数化查询来防止 SQL 注入,因此请更改您的查询逻辑:

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();

对此:

string strsql = "select * from UserInfo,UsersGroup  where UserInfo.GroupID=UsersGroup.GroupID and UserName = @UserName and UserPassword=@UserPassword";

cmd = new SqlCommand(strsql, conn);
cmd.AddParameterWithValue("@UserName", UserName);
cmd.AddParameterWithValue("@UserPassword", txtusername.Text);

conn.Open();

dr = cmd.ExecuteReader();
于 2012-10-10T11:51:23.650 回答
0

请查阅SQL Server 网络配置下的 SQL Server 配置管理器,您将找到协议。确保 TCP/IP 已启用。初始安装后,通常禁用此功能。

于 2012-10-10T11:53:33.490 回答