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;
using System.Configuration;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
SqlConnection conn; public Form1()
{
InitializeComponent(); //回车事件处理
this.textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown); //数据库连接字符串 conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
conn.Open();
} //回车方法
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.button1_Click(button1, null);
}
}
//检测重码
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length == 0)
{
MessageBox.Show("输入不能为空!");
}
else
{
string code = textBox1.Text;
SqlCommand cmd = new SqlCommand("insert into codeTable values('" + code + "')", conn);
try
{
int count = (int)cmd.ExecuteNonQuery();
if (count > 0)
{
listBox1.Items.Add(code + " 已录入");
}
else
{
listBox1.Items.Add(code + " 录入失败");
}
}
catch
{
MessageBox.Show(code + " 已经存在!");
}
finally
{
textBox1.Text = "";
}
}
}以上是代码,每次输入东东都是显示,XXX已录入,相同的也有已存在的提示。关闭之后,数据库里面一看,还是没变化。
求解?不晓得啥米情况,这个数据库是用VS自带的mdf数据库。
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;
using System.Configuration;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
SqlConnection conn; public Form1()
{
InitializeComponent(); //回车事件处理
this.textBox1.KeyDown += new KeyEventHandler(textBox1_KeyDown); //数据库连接字符串 conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True");
conn.Open();
} //回车方法
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
this.button1_Click(button1, null);
}
}
//检测重码
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length == 0)
{
MessageBox.Show("输入不能为空!");
}
else
{
string code = textBox1.Text;
SqlCommand cmd = new SqlCommand("insert into codeTable values('" + code + "')", conn);
try
{
int count = (int)cmd.ExecuteNonQuery();
if (count > 0)
{
listBox1.Items.Add(code + " 已录入");
}
else
{
listBox1.Items.Add(code + " 录入失败");
}
}
catch
{
MessageBox.Show(code + " 已经存在!");
}
finally
{
textBox1.Text = "";
}
}
}以上是代码,每次输入东东都是显示,XXX已录入,相同的也有已存在的提示。关闭之后,数据库里面一看,还是没变化。
求解?不晓得啥米情况,这个数据库是用VS自带的mdf数据库。
解决方案 »
- 同时创建多个socket如何解决?
- 类qq的聊天程序,好友上下线时,如何只更新上线的好友的状态
- 怎么获取到数据库sql2000 事务日志中执行过sql语句( 查询 修改 删除 存储过程等)
- 怎样获得dataGridView的单元格坐标并画线
- 菜鸟问题,请求帮助!!!
- dateTimePicker1里面 tostring 参数有几个?
- 各位高手,关于安全性的困惑,高分求解,解决马上结贴
- 求一个SQL SERVER2000和VS2005环境下的WEB程序源码
- 在gridcontrol如何像EXCEL那个任意合并单元格
- 我郁闷啊!散分...
- 各位大侠请进
- TableLayouPanel动态添加控件奇怪问题
{
}
insert前判断数据是否存在
呃偷懒了一下,没有装SQL,直接用VS的自带的数据库。该不会就是这里有问题吧?
2,如果conn是Open的,并且程序已经提示xx已更新,那就说明应该程序没问题,就是sql更新返回了受影响的行数。
那么检查你的数据库是否有触发器,当执行插入操作的时候自动进行删除或者数据库是否只读?