就是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);
}
}
}
}
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);
}
}
}
}
解决方案 »
- SQL表導出到EXCEL時,提示: OLE DB 提供者 "Microsoft.Jet.OLEDB.4.0" 尚未註冊
- 如果有两个Form页面,在Form2中增加一条数据时,怎样才能使Form1的状态发生改变
- C# winfrom webbrowser 打印问题
- 创建wpf popup对话框(winform)
- 使用ItextSharp解析PDF内容流时,如何解析为中文(急,在线等)
- 100分求网站设计思路.....
- 重载"ExEcWB"方法未获取"2"参数
- 怎么给视频(格式如wmv,mov,rm,rmvb等)添加一帧图片呢?
- 考试题,在线等高手作答!
- 在javascript中怎么样定义一个两维数组?
- cs中如何把改变Label中指定位置的文本的颜色其它的不变
- 又谈c#winfrom程序窗体互动问题。
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 ...
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);
}
}
conn.close()上面点结尾(呵呵,跑题了!)
//要防止插入重复数据 看样子要分两步:1.从数据库中找输入的数据是否存在!
2.插入数据 (可能好理解些,呵呵。。)意思就是说 sql语句要写两次!1.查找 2.插入数据因为都是新手,呵呵 最好先写好"查询"代码 (问下:知道应该要什么类型接收吗?、如int,object)
还有个很严重的问题!我今天肚子不舒服啦(呵呵) 用于输出提示消息的部分代码应该写在 最后面
写在try里面的话“基本上就没什么作用了"没事的话多用个关键子 return (跳出事件或方法)
比如说:if(查到数据存在)
{
mbox("提示之类滴"); //1
return; //2
} (结果就是事件在 //2 就停止啦!!) 讲起来,做项目做不出不要紧嘞~~正所谓 大难不死,接着搞!!!呵呵