就是else if中的东西不知道怎么写,请高手帮忙指出修改,谢谢
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.Data.SqlClient;namespace insert_user_info
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            string str = "server=192.168.1.40;Initial Catalog=login_test;uid=sa;pwd=123" ;
            try
            {
                SqlConnection conn = new SqlConnection(str);
                conn.Open();
                
                string sql = "insert into userinfo values('" + textBox1.Text.Trim() + "','" + textBox2.Text.Trim() + "','" + textBox3.Text.Trim() + "','" + textBox4.Text.Trim() + "')";
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "")
                {
                    cmd.CommandText = sql;
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("数据添加成功");
                }
                else if (cmd.CommandText = "select username from userinfo where username='" + textBox1.Text.Trim() + "'" = cmd.ExecuteNonQuery())
                {
                    MessageBox.Show("你所要添加的用户信息已经存在请重新添加");
                }
                else
                {
                    MessageBox.Show("请将信息填写完整");
                }
                conn.Close();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
           
        }
    }
}

解决方案 »

  1.   

    先查询是否存在再insert
    bool flag=false;
    string sql = "select count(*) from userinfo  where username = '"+textBox1.Text+"'"; 
    int count = Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.Conn, CommandType.Text, sql)); 
    if (count > 0) 
        flag = true;
    else 
        flag = false;

    if   notexists(   select   id   from   table   where   id   =   "id "   ) 
      insert   into   ... 
      

  2.   

    代码的结构需要调整
    private void button1_Click(object sender, EventArgs e) 

    if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "") 
            { 
    MessageBox.Show("请将信息填写完整"); 
    return;
    } string str = "server=192.168.1.40;Initial Catalog=login_test;uid=sa;pwd=123" ; 
            try 
            { 
                    SqlConnection conn = new SqlConnection(str); 
                    conn.Open(); 
    string sql = "select count(*) from userinfo where username where username='{0}'";
    sql = String.Format(sql,textBox1.Text.Trim());

    SqlCommand cmd = new SqlCommand(sql,conn);
    object obj = cmd.ExecuteScalar();
    if ( Convert.ToInt32(obj) > 0 )
    {
    MessageBox.Show("你所要添加的用户信息已经存在请重新添加"); 
    return ;
    }

    //以下为添加用户信息代码
    //...     } 
                catch(Exception ex) 
                { 
                    MessageBox.Show(ex.Message); 
                } 
    }
      

  3.   

    //代码 try 滴范围太大了(不规范嘞!) 数据库应用程序一般出现异常的地方就是conn.open()开始到
    conn.close()上面点结尾(呵呵,跑题了!)
    //要防止插入重复数据 看样子要分两步:1.从数据库中找输入的数据是否存在!
                                          2.插入数据  (可能好理解些,呵呵。。)意思就是说 sql语句要写两次!1.查找 2.插入数据因为都是新手,呵呵 最好先写好"查询"代码 (问下:知道应该要什么类型接收吗?、如int,object)
          
    还有个很严重的问题!我今天肚子不舒服啦(呵呵) 用于输出提示消息的部分代码应该写在 最后面
                                                 写在try里面的话“基本上就没什么作用了"没事的话多用个关键子 return (跳出事件或方法)
    比如说:if(查到数据存在)
    {
    mbox("提示之类滴");  //1
    return;              //2
    }     (结果就是事件在 //2 就停止啦!!)     讲起来,做项目做不出不要紧嘞~~正所谓 大难不死,接着搞!!!呵呵