**星*星** 2022-11-27 08:54 采纳率: 66.7%
浏览 17

用VS软件中的C#连接数据库,两天了一直弄不好,有没有人可以帮帮我?

问题遇到的现象和发生背景

用C#做一个可以连接数据库的登录页面

用代码块功能插入代码,请勿粘贴截图
    readonly string Stu = ConfigurationManager.ConnectionStrings["sqlserver"].ConnectionString;     //创建数据库连接
    public static string username;  //设置Form1的一个参数,登录成功后将显示在主界面Form3
    public static string password;
    private void Button1_Click(object sender, EventArgs e)  //登录代码
    {
        SqlConnection conn = new SqlConnection(Stu);    //创建数据库游标对象conn
        conn.Open(); //打开数据库
        SqlCommand cmd = conn.CreateCommand();
        cmd.CommandText = "select * from 登录注册表 where username='" + textBox1.Text.Trim() + "'";
        SqlDataReader sdt = cmd.ExecuteReader();
        if (textBox1.Text == "")
        {
            MessageBox.Show("账号不得为空!", "提示");
            this.ActiveControl = textBox1;
        }
        else if (textBox2.Text == "")
        {
            MessageBox.Show("密码不得为空!", "提示");
            this.ActiveControl = textBox2;
        }
        else if (sdt.Read() == true)    //读取到记录
        {
            string result = sdt.GetString(sdt.GetOrdinal("password"));
            if (MD5Encrypt(textBox2.Text.Trim(), textBox1.Text.Trim()) == result)
            {
                MessageBox.Show("登录成功!", "提示");
                username = textBox1.Text.Trim();       //将textBox1的值赋给参数username
                password = textBox2.Text.Trim();        //将textBox2的值赋给参数password
                conn.Close();         //关闭数据库连接
                this.Hide();         //隐藏当前窗口
                Form3 sd = new Form3();
                sd.Show();         //显示主界面Form3
            }
            else
            {
                MessageBox.Show("账号密码错误!", "提示");
                textBox2.Text = "";
                this.ActiveControl = textBox2;
            }
        }
        else
        {
            DialogResult dr = MessageBox.Show("账号不存在,是否选择注册一个新账号?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (dr == DialogResult.OK)
            {
                this.Hide();
                Form2 er = new Form2();
                er.Show();
                conn.Close();
            }
            else
            {
                textBox1.Text = "";
                textBox2.Text = "";
                this.ActiveControl = textBox1;
            }
        }
        conn.Close();   //关闭游标对象连接
    }
运行结果及报错内容

readonly string Stu = ConfigurationManager.ConnectionStrings["sqlserver"].ConnectionString; //创建数据库连接
本行报错:System.NullReferenceException:“未将对象引用设置到对象的实例。”
System.Configuration.ConnectionStringSettingsCollection.this[string].get 返回 null。

我的解答思路和尝试过的方法

        this.button1.Font = new System.Drawing.Font("宋体", 15F);
        this.button1.Location = new System.Drawing.Point(287, 232);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(118, 38);
        this.button1.TabIndex = 5;
        this.button1.Text = "登录";
        this.button1.UseVisualStyleBackColor = true;

改成

        this.button1.Font = new System.Drawing.Font("宋体", 15F);
        this.button1.Location = new System.Drawing.Point(287, 232);
        this.button1.Name = "button1";
        this.button1.Size = new System.Drawing.Size(118, 38);
        this.button1.TabIndex = 5;
        this.button1.Text = "登录";
        this.button1.UseVisualStyleBackColor = true;
        this.button1.Click += new System.EventHandler(this.button1_Click1);

运行不了

我想要达到的结果

我想知道怎样解决这个问题

  • 写回答

2条回答 默认 最新

  • 我可是森森呢 2022-12-01 11:24
    关注
    给你个C#连接SqlServer数据库查询语句的简单实例吧,dt.Rows[行下标]["列名"]就能取值
    
    ```c#
                DataTable dt = new DataTable();
                string sql = "select * from 表";
                SqlConnection con = new SqlConnection($"Data Source=127.0.0.1;Initial Catalog=数据库名;User ID=账号;Password=密码;Connect Timeout=5");
                con.Open();
                SqlDataAdapter dat = new SqlDataAdapter(sql, con);
                dat.Fill(dt);
                con.Close();
                //到这里,dt里存的就是查到的数据
    
    

    ```

    评论

报告相同问题?

问题事件

  • 创建了问题 11月27日

悬赏问题

  • ¥100 ensp启动设备蓝屏,代码clock_watchdog_timeout
  • ¥15 Android studio AVD启动不了
  • ¥15 陆空双模式无人机怎么做
  • ¥15 想咨询点问题,与算法转换,负荷预测,数字孪生有关
  • ¥15 C#中的编译平台的区别影响
  • ¥15 软件供应链安全是跟可靠性有关还是跟安全性有关?
  • ¥15 电脑蓝屏logfilessrtsrttrail问题
  • ¥20 关于wordpress建站遇到的问题!(语言-php)(相关搜索:云服务器)
  • ¥15 【求职】怎么找到一个周围人素质都很高不会欺负他人,并且未来月薪能够达到一万以上(技术岗)的工作?希望可以收到写有具体,可靠,已经实践过了的路径的回答?
  • ¥15 Java+vue部署版本反编译