m0_53904687 2021-09-23 18:42 采纳率: 100%
浏览 526
已结题

“System.Data.SqlClient.SqlException”类型的未经处理的异常在 System.Data.dll 中发生 其他信息: 列名 'Name' 无效。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace ShuJuKu
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection contest; //连接数据库对象
SqlCommand cmdLoginOP; //执行SQL命令对象
string cmdString; // Sql语句字符串
//判断指定的用户名是否存在,如果存在返回true,否则返回false
bool IsExist(string Name)
{
string str = string.Format("select count(*) from Table_2 where Name='{0}'",Name);
cmdLoginOP = new SqlCommand(str,contest);
contest.Open();
int count = Convert.ToInt32(cmdLoginOP.ExecuteScalar()); //查询该用户名的个数
contest.Close();
if (count!=0)
{
return true;
}
return false;
}
//执行Sql命令,执行成功返回true,否则返回false
bool GetSqlCmd(string CmdStr)
{
cmdLoginOP = new SqlCommand(CmdStr, contest);
contest.Open();
int count = cmdLoginOP.ExecuteNonQuery(); //执行SQL命令并返回受影响行数
contest.Close();
if (count!=0)
{
return true;
}
return false;
}

    private void Form1_Load(object sender, EventArgs e)
    {
        string ConString = "server=.;database=test;uid=sa;pwd=sa;";
        contest = new SqlConnection(ConString);
    }
    //选项卡选择页面更改事件执行代码
    private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (tabControl1.SelectedIndex==0)
        {
            groupBox1.Parent = tabControl1.TabPages[0]; //将组中对象放入第一个页面
        }
        if (tabControl1.SelectedIndex == 1)
        {
            groupBox1.Parent = tabControl1.TabPages[1]; //将组中对象放入第二个页面

        }
    }

    //保存按钮单击事件执行代码
    private void button1_Click(object sender, EventArgs e)
    {
        if (textBox1.Text==string.Empty||textBox2.Text==string.Empty)
        {
            MessageBox.Show("用户名和密码不能为空");
            return; //中止该方法
        }
        string StuName = textBox1.Text.Trim();
        string StuPass = textBox2.Text.Trim();
        if (tabControl1.SelectedIndex==0)  //组合框内的控件在添加页面
        {
            cmdString = string.Format("insert into Table 2 values('{0}','{1}')", StuName, StuPass);
        }
        if (IsExist(StuName))
        {
            MessageBox.Show("对不起,该用户名已经存在");
        }
        else
        {
            if (GetSqlCmd(cmdString))
            {
                MessageBox.Show("添加用户成功");
                textBox1.Text="";
                textBox2.Text = "";
                textBox1.Focus();  //获取光标                 
            }
        }
    }

    //退出按钮事件执行代码
    private void button2_Click(object sender, EventArgs e)
    {
        contest.Dispose();
        this.Close();
    }
    //删除按钮单击事件执行代码
    private void button3_Click(object sender, EventArgs e)
    {
        string StuName = textBox3.Text.Trim();
        cmdString = string.Format("delete Table_2 where Name='{0}'",StuName);
        if (!IsExist(StuName))
        {
            MessageBox.Show("对不起,该用户不存在");
        }
        else
        {
            if (GetSqlCmd(cmdString))
            {
                MessageBox.Show("删除用户成功!");
                textBox3.Text = "";
                textBox3.Focus(); //获取光标
            }
        }
    }





    














}

}

img

img

img

  • 写回答

1条回答 默认 最新

  • CSDN专家-showbo 2021-09-23 20:19
    关注

    题主你的Table_2的列明是张三,不是Name列。。改下数据库字段名为Name

    img


    有帮助麻烦点个采纳【本回答右上角】,谢谢~~有其他问题可以继续交流~

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 10月2日
  • 已采纳回答 9月24日
  • 创建了问题 9月23日

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器