INSERT t_rbasedata(除sortid 的所有列 ) SELECT 除sortid 的所有列 FROM t_rbasedata a WHERE NOT EXISTS(SELECT 1 FROM t_rbasedatah)
SqlConnection con = new SqlConnection("Data Source=10.168.1.5;Initial Catalog=data;User ID=sa;password=sa;Integrated Security=False"); con.Open(); SqlCommand cmd = new SqlCommand(string.Format("select Count(*) from newtable where a= '{0}'", s1), con); if ((int)cmd.ExecuteScalar() > 0) { listBox1.Items.Add(s1 + " 数据已经存在"); } else { string sql = "insert into newtable(a,b,c) values('" + s1 + "','" + s2 + "','" + s3 +"')"; cmd.CommandText = sql; cmd.ExecuteNonQuery(); listBox1.Items.Add(s1 + " 成功添加"); } cmd.Dispose(); con.Close();
编写proc,传入sortID,执行 select count(sortID) from table where sortID = 参数 执行proc获取返回值判断。
在trans里面建立嵌套这个不行诶 会报错
你写个函数,返回记录数, SqlConnection con = new SqlConnection("Data Source=10.168.1.5;Initial Catalog=data;User ID=sa;password=sa;Integrated Security=False"); con.Open(); SqlCommand cmd = new SqlCommand(string.Format("select Count(*) from newtable where a= '{0}'", s1), con); int i=(int)cmd.ExecuteScalar() ; 有记录了就不入库了
我写了一个函数 public static int getrecord(string sortid) { SqlConnection iconn = returnSqlCon(); iconn.Open(); string qstring = "select count(*) from t_rbasedata where sortID='" + sortid + "'"; SqlCommand mysc = new SqlCommand(qstring, iconn); int mi = (int)mysc.ExecuteScalar(); return mi; } 调用这个函数第一次循环可以正常进行,但是第二次循环无法进行下去呢,最后提示超时时间已到...要知道后面可是有可能会有十几、二十几次循环呢
con.Open();
SqlCommand cmd = new SqlCommand(string.Format("select Count(*) from newtable where a= '{0}'", s1), con);
if ((int)cmd.ExecuteScalar() > 0)
{
listBox1.Items.Add(s1 + " 数据已经存在");
}
else
{
string sql = "insert into newtable(a,b,c) values('" + s1 + "','" + s2 + "','" + s3 +"')";
cmd.CommandText = sql;
cmd.ExecuteNonQuery();
listBox1.Items.Add(s1 + " 成功添加");
}
cmd.Dispose();
con.Close();
执行proc获取返回值判断。
con.Open();
SqlCommand cmd = new SqlCommand(string.Format("select Count(*) from newtable where a= '{0}'", s1), con);
int i=(int)cmd.ExecuteScalar() ;
有记录了就不入库了
{
SqlConnection iconn = returnSqlCon();
iconn.Open();
string qstring = "select count(*) from t_rbasedata where sortID='" + sortid + "'";
SqlCommand mysc = new SqlCommand(qstring, iconn);
int mi = (int)mysc.ExecuteScalar();
return mi; } 调用这个函数第一次循环可以正常进行,但是第二次循环无法进行下去呢,最后提示超时时间已到...要知道后面可是有可能会有十几、二十几次循环呢
即使执行了几百次都不出错,总有数据库连接池满的时候
//输入框设置事件判断
<asp:TextBox ID="txtName" runat="server" BackColor="White" Height="20px" MaxLength="8"
Rows="1" Width="150px" AutoPostBack="True" onFocus="tName();" OnTextChanged="txtName_TextChanged"></asp:TextBox>
//显示相应
<span id="sp1" style="font-size: 12px; color: Blue"></span>
<asp:Label ID="labIsName" runat="server" Font-Size="12px"></asp:Label>
//cs页面调用事件判断
protected void txtName_TextChanged(object sender, EventArgs e)
{ //调用自定义isNameFormar方法判断用户名是否满足格式要求
if (isNameFormar())
{
//调用isName自定义方法判断用户名是否已注册
if (isName())
{
labIsName.Text = "用户名已存在!";
labIsName.ForeColor = System.Drawing.Color.Red;
}
else
{
labIsName.Text = "可以注册!";
labIsName.ForeColor = System.Drawing.Color.Blue;
}
}
else
{
labIsName.Text = "";
}
}//codego.net/tags/11/1/
//判断用户名是否存在
protected bool isName()
{
//创建一个布尔型变量并初始化为false;
bool blIsName = false;
//创建SQL语句,该语句用来判断用户名是否存在
string sqlSel = "select count(*) from tb_HuenLian where UserName='" + txtName.Text + "' ";
//创建数据库连接
SqlConnection con = new SqlConnection(strCon);
//打开数据库连接
con.Open();
//创建SqlCommand对象
SqlCommand com = new SqlCommand(sqlSel, con);
//判断ExecuteScalar方法返回的参数是否大于0,大于表示用户名已存在
if (Convert.ToInt32(com.ExecuteScalar()) > 0)
{
blIsName = true;
}
else
{
blIsName = false;
}
//返回布尔值变量
return blIsName; }
//注册时加载验证是否同名存在
if (isName()) //调用自定义方法判断用户名是否存在
{
//使用Label控件显示提示信息
labIsName.Text = "用户名已存在!";
//设置Label控件的颜色
labIsName.ForeColor = System.Drawing.Color.Red;
RegisterStartupScript("", "<script>alert('请正确填写信息!')</script>");
}//其他插入信息(略)