我要根据一个文本框修改,但要判断这个文本框的字段是否在数据库存在?方法要怎么写啊?谢谢了!

解决方案 »

  1.   


    string str=this.文本框名字.text;根据文本框的名字(str)去数据库对应的表查嘛、
      

  2.   

    "select 1 from table where XX='" & textbox1.text & "'"
      

  3.   

    楼主不会写Sql语句还是哪里出了问题  
      

  4.   

            if(txtWorkCode.Text == "")
                {
                    MessageBox.Show("请先输入需要修改信息的运编号!");
                    return;
                }
                ModifyEditForm mef = new ModifyEditForm(txtWorkCode.Text.ToUpper());
                mef.ShowDialog();
                if (mef.DialogResult == DialogResult.OK)
                {
                    BindGrid();
                }
    我想把这个ModifyEditForm mef = new ModifyEditForm(txtWorkCode.Text.ToUpper());
    判断一下是否存在.如果不存在就不能跳转!
      

  5.   

    SQL语句,和表中的数据对比,不行? 
      

  6.   

    代码怎么写这是其次,因为你是新手,我给你缕缕思路:
    要判断数据在数据库库是否存在:
    首先,你要获取文本框的内容(因为你要对比嘛)
    其次,要把这个字符串的值与数据库比对(所以你要写SQL语句)
    最后,根据数据库比对结果看返回值(因为你向数据库查询,必须得知道有没有呀)
      

  7.   

    代码很简单,一个sql语句到数据库去查询就行了,要自己多试一试,印象才深刻
      

  8.   

    DataSet获取"select * from DB where id=xxx"。
    然后判断DataSet的DataTable的Columns里有没有该字段。
      

  9.   

    String.Format("select * from table1 where column1 = '{0}'", textBox1.Text);
      

  10.   

    最近也在学ajax和jquery
    遇到和楼主一样问题,我把自己的笨蛋代码贴上来可以 参考下:
    string querystring = Request["UserName"].ToString();
            querystring = Server.UrlDecode(querystring);
            string connectString = ConfigurationManager.ConnectionStrings["数据库连接"].ConnectionString;
            SqlConnection conn = new SqlConnection(connectString);        try
            {
                conn.Open();
             
                conn.Close();        }
            catch (SqlException ee)
            {
                Response.Write(ee.Message.ToString());        }
        
            conn.Open();
            string Checksql = "select 用户名 from TableUserInform ";
            int tag = 0;
            string xx = null;
            SqlCommand checkcmd = new SqlCommand(Checksql, conn);
            SqlDataReader dr = checkcmd.ExecuteReader();
        
            while (dr.Read())
            {
               
              
                xx = dr["用户名"].ToString();
               
                if (xx == querystring)
                {                tag = 1;
                    break;
                 }
            }
            if (querystring != ""&&querystring!=null)
            {            if (tag == 1)
                {
                    Response.Write("<b style='color=red'>!用户名已存在</b>");            }
                else if (tag == 0)
                {
                    Response.Write("<b style='color=green'>√用户名可注册</b>");
                    ///插入数据库;
                }
            }
            else
            {
                Response.Write("<b style='color=yellow'>请输入用户名</b>");
            }
            conn.Close();
      

  11.   

    最近也在学ajax和jquery
    遇到和楼主一样问题,我把自己的笨蛋代码贴上来可以 参考下:
    string querystring = Request["UserName"].ToString();
            querystring = Server.UrlDecode(querystring);
            string connectString = ConfigurationManager.ConnectionStrings["数据库连接"].ConnectionString;
            SqlConnection conn = new SqlConnection(connectString);        try
            {
                conn.Open();
             
                conn.Close();        }
            catch (SqlException ee)
            {
                Response.Write(ee.Message.ToString());        }
        
            conn.Open();
            string Checksql = "select 用户名 from TableUserInform ";
            int tag = 0;
            string xx = null;
            SqlCommand checkcmd = new SqlCommand(Checksql, conn);
            SqlDataReader dr = checkcmd.ExecuteReader();
        
            while (dr.Read())
            {
               
              
                xx = dr["用户名"].ToString();
               
                if (xx == querystring)
                {                tag = 1;
                    break;
                 }
            }
            if (querystring != ""&&querystring!=null)
            {            if (tag == 1)
                {
                    Response.Write("<b style='color=red'>!用户名已存在</b>");            }
                else if (tag == 0)
                {
                    Response.Write("<b style='color=green'>√用户名可注册</b>");
                    ///插入数据库;
                }
            }
            else
            {
                Response.Write("<b style='color=yellow'>请输入用户名</b>");
            }
            conn.Close();
      

  12.   

     后台代码什么的都有了,取到的值也为空了,就是判断出了问题?
      var  tet=bmm.GetWorkCodeOne(txtWorkCode.Text);
                if (tet == null) 
                {
                    MessageBox.Show("请输入正确的运编号!");
                    return;
                }
    这个IF怎么写啊,谢谢了!
      

  13.   

     连接字符串在web.config中配置
    string connectString = ConfigurationManager.ConnectionStrings["数据库连接"].ConnectionString;
      SqlConnection conn = new SqlConnection(connectString);
    public void TableSelect()
        {
            conn.Open();
            string condition5=textbox.Text;
            string selectStr = "select count(*) from TableUserInform where 用户名='" + condition5 + "'";
            SqlCommand cmd = new SqlCommand(selectStr, conn);        string tag=cmd.ExecuteScalar().ToString();
            Response.Write("找到的匹配项为:" + tag + "项");
            conn.Close();
        }
      

  14.   


    SqlConnection conn = new SqlConnection("server=127.0.0.1;database=testdb;uid=sa;pwd=123");
    conn.Open();
    string sql="select count(*) from test_tb where textbox='"+TextBox1.Text+"'";
    SqlCommand cmd = new SqlCommand(sql, conn);
    int k=Convert.ToInt32(cmd.ExecuteScalar())
    if(k>0)
    {
      MessageBox.show("数据库的值已经存在");
    }
    else
    {
      MessageBox.show("数据库的值bu存在"); 
    }