主要在Leave事件中编写。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Text; using System.Windows.Forms;namespace WindowsApplication6 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void textBox1_Leave(object sender, EventArgs e) { string str = @"数据库连接字符串"; SqlConnection conn = new SqlConnection(str); SqlCommand cmd = new SqlCommand("select * from 表名 where 字段名 =" + textBox1.Text.Trim(), conn); DataSet ds=new DataSet(); SqlDataAdapter da=new SqlDataAdapter(cmd); conn.Open(); da.Fill(ds,"表名"); DataTable dt=ds.Tables["上一行中的表名"]; textBox2.Text = dt.Rows[0][1].ToString(); } } }
如果要無刷新就用AJAX
{
if(!string.isNullOrEmpty(textBox1.Text))
{
string sql="select * from tablename where filedname='"+textBox1.Text+"' ";
..执行查询
if(有值)
{
MessageBox.Show("该值已经存在!");
}
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void textBox1_Leave(object sender, EventArgs e)
{
string str = @"数据库连接字符串";
SqlConnection conn = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("select * from 表名 where 字段名 =" + textBox1.Text.Trim(), conn);
DataSet ds=new DataSet();
SqlDataAdapter da=new SqlDataAdapter(cmd);
conn.Open();
da.Fill(ds,"表名");
DataTable dt=ds.Tables["上一行中的表名"];
textBox2.Text = dt.Rows[0][1].ToString();
}
}
}
{
string str = @"数据库连接字符串";
SqlConnection conn = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("select * from 表名 where 字段名 =" + textBox1.Text.Trim(), conn);
DataSet ds=new DataSet();
SqlDataAdapter da=new SqlDataAdapter(cmd);
conn.Open();
da.Fill(ds,"表名");
DataTable dt=ds.Tables["上一行中的表名"];
textBox2.Text = dt.Rows[0][1].ToString();
}
使用如下代码后
好象我在运行程序的时候好象没有执行这个代码呢!!!!
我在textBox1_Leave代码中加入了一句textBox1.Enabled = false;
当光标离开textbox1的时候 textbox1都没有反应呢~~~~