c# - 数据类型 text 和 varchar 在等于运算符中不兼容

标签 c# database winforms sqldatareader

我正在尝试访问数据 GridView 中数据库中的所有记录,具体取决于通过分别具有 2 个用户名和密码文本框的表单登录的用户类型,并显示提交按钮记录。但是我编写的代码出现以下错误:数据类型 text 和 varchar 在等于运算符中不兼容。请提出更改建议。

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


namespace Login_form
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {



        }

        private void button1_Click(object sender, EventArgs e)
        {

            string str = ConfigurationSettings.AppSettings["constring"].ToString();
            SqlConnection sqlcon = new SqlConnection(str);


            try
            {
                sqlcon.Open();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }


            SqlCommand sqlcmd = new SqlCommand("select user_type from employee where user_name='" + textBox1.Text + "'and pwd= '" + textBox2.Text + "' ;", sqlcon);


            SqlDataReader myReader = sqlcmd.ExecuteReader();

            string user_type=string.Empty;

            while(myReader.Read())
            {
            user_type= myReader["user_type"].ToString();
            }

            sqlcon.Close();



            sqlcon.Open();
            SqlCommand sqlcmd2 = new SqlCommand("select * from employee where user_type= '" +user_type + "'", sqlcon);
            SqlDataReader myReader2 = sqlcmd2.ExecuteReader();

          /*  SqlDataAdapter sqladapter = new SqlDataAdapter(sqlcmd2);
            DataSet ds = new DataSet();
            sqladapter.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];*/

            DataTable dt = new DataTable();
            dt.Columns.Add("ID", typeof(string));
            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Email_ID", typeof(string));
            dt.Columns.Add("Contact", typeof(string));
            dt.Columns.Add("Address", typeof(string));

            while (myReader2.Read())
            {
                DataRow dr = dt.NewRow();
                dr["ID"] = myReader2["ID"];
                dr["Name"] = myReader2["user_name"];
                dr["Email_ID"] = myReader2["Email_ID"];
                dr["Contact"] = myReader2["Contact"];
                dr["Address"] = myReader2["Address"];
                dt.Rows.Add(dr);
            }


            dataGridView1.DataSource = dt;


            sqlcon.Close();
        }

    }
}

最佳答案

这是一个简单的数据库问题。

在数据库生成脚本中更改:

columnname text NULL,

到:

columnname varchar(number of chars) NULL,

在您的情况下,列名将是“user_name”或“user_type”

出现此问题是因为 SQL 类型的文本!与字符串比较不兼容!

只要将类型从 text 更改为 varchar(),该操作就可以运行

关于c# - 数据类型 text 和 varchar 在等于运算符中不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15133094/

相关文章:

c# - WinForms:响应正在应用的 BindingSource

java - Hello World hibernate

c# - 带搜索框的多线程

c# - 使用 .NET/C# 将 byte[] 转换为 XML

c# - ADO.NET 与 EntityFramework

mysql - 如何更改时间戳的默认日期和时间格式,如 2015-09-29 10 :48:33 To 2015:09:29 10:48:33

database - 多次使用 Test::MockDBI 得到不同的结果

c# - 为什么我会收到 ""对象“不包含 "Index"的定义”错误?

c# - 在 Windows 窗体应用程序中实现单词拼写检查

c# - 屏幕抓取 : unable to authenticate into a site utilizing ASP . NET Forms 身份验证