用C#查sql库中一个表是否有记录(送分的题)

grt 2008-07-09 09:59:06
用C#写,查sql2000库中一个表是否有指定的记录,主键ID=10的,如果没有就插入记录,如果有就提示信息:已存在。
用c#具体写没写过。请帮助。
...全文
254 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
grt 2008-07-15
  • 打赏
  • 举报
回复
22楼的朋友,帮我解决了消息框问题。
其它朋友都说了一点边,没真正解决,还有感谢大家,分少就少一点吧。

我自己把它搞的简单,又解决问题。
grt 2008-07-15
  • 打赏
  • 举报
回复
我自己全部搞定,你们没一个说对的。
grt 2008-07-11
  • 打赏
  • 举报
回复
24楼,你照抄我一遍?

出错提示:必须声明变量@IDD
xuyang840117 2008-07-11
  • 打赏
  • 举报
回复
@bol NVARCHAR(50) OUTPUT

SELECT @NO = count(id) FROM 表 WHERE id= '10'

IF @NO <> '0'
BEGIN

INSERT INTO 表 VALUES ('','',''...)

set @bol = '1'
END
ELSE
BEGIN
set @bol = '0'
END

代码实现 如果返回 0 提示 存在
89993004 2008-07-11
  • 打赏
  • 举报
回复
SqlConnection conn=new SqlConnection("YourConnectionString");
sql="select count(*) from table1 whete id=10";
SqlCommand cmd=new SqlCommand(sql,conn);
conn.open();
int result=(int)cmd.ExecuteNoneQuery();
conn.close();
if(result>0)
{
执行插入...
}


xuqunying0545 2008-07-10
  • 打赏
  • 举报
回复
同上
grt 2008-07-10
  • 打赏
  • 举报
回复
可能,我试一下
我很懒 2008-07-10
  • 打赏
  • 举报
回复
SqlDataSource SqlDataSource4 = new SqlDataSource();
SqlDataSource4.ConnectionString = "Data Source=SERVER2;Initial Catalog=anjz;Integrated Security=True";
SqlDataSource4.SelectCommand = "select count(*) from khjg where IDD = @IDD";
SqlDataSource4.SelectParameters.Add("IDD", TypeCode.Int32, IDDDd.ToString());
SqlConnection conn = new SqlConnection(SqlDataSource4.ConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand(SqlDataSource4.SelectCommand.ToString(),conn);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
Response.Write(" <script>alert('该记录已存在!'); </script>");
}
else
{insert.....}
}
brooklyng60 2008-07-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ojlovecd 的回复:]
C# code
using(SqlConnection conn = new SqlConnection("连接字符串"))
{
conn.Open();
SqlCommand cmd = new SqlCommand("select * from table where ID=10",conn);
SqlDataReader sdr =new SqlDataReader ();
sdr =cmd.ExecuteReader();
if(sdr.Read())
//进行数据插入操作
else
MessageBox.Show("已经存在该数据");
}
[/Quote]
cooolchen 2008-07-09
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 grt 的回复:]
9 楼朋友:
你的这句:SqlDataReader sdr = cmd.ExecuteReader();
出现如下提示:
ExecuteReader: Connection 属性尚未初始化。
[/Quote]

SqlCommand cmd = new SqlCommand(SqlDataSource1.SelectCommand.ToString(),con);
grt 2008-07-09
  • 打赏
  • 举报
回复
9 楼朋友:
你的这句:SqlDataReader sdr = cmd.ExecuteReader();
出现如下提示:
ExecuteReader: Connection 属性尚未初始化。
我姓区不姓区 2008-07-09
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 grt 的回复:]
朋友们:
你们说的都是:WinForm 中的语言,我是webForm 中用C#不太一样吧?特别是消息框。
[/Quote]
那弹出消息框可以这么写:

ClientScript.RegisterStartupScript(this.GetType(), "", "alert('已经存在该信息');", true);
liuhz_jsmstc 2008-07-09
  • 打赏
  • 举报
回复
using(SqlConnection sqlcon = new SqlConnection("连接字符串"))
{
sqlcon.Open();
string sqlstr="select * from table where ID=10";
SqlDataAdapter sqladp=new SqlDataAdapter(sqlstr,sqlcon);
DataSet ds=new DataSet();
sqladp.Fill(ds);
if (ds.Table[0].Rows.Count > 0)
{
string insstr = "insert into table values()";
SqlCommand sqlcmd = new SqlCommand(insstr, sqlcon);
sqlcmd.EndExecuteNonQuery();
}
else
{
MessageBox.Show("己添加");
}
}
grt 2008-07-09
  • 打赏
  • 举报
回复
朋友们:
你们说的都是:WinForm 中的语言,我是webForm 中用C#不太一样吧?特别是消息框。
RichardSzwz 2008-07-09
  • 打赏
  • 举报
回复

//连接字符串
string connectionSting = @"Data Source=Source;Initial Catalog=database;Persist Security Info=True;User ID=sa;Password=password";
SqlConnection myConnection = new SqlConnection(connectionSting);
//打开数据库
myConnection.Open();

//检查有无数据
DataSet ds = new DataSet();
string oda = "SELECT * FROM table where id=10;
SqlDataAdapter df = new SqlDataAdapter(oda, myConnection);
df.Fill(ds, "tablename");
if (ds.Tables[第几列].Rows.Count > 0)
{
MessageBox.show("数据存在!")
}


我正用的代码!稍加修改参数即可使用!
xlyhj219 2008-07-09
  • 打赏
  • 举报
回复
public int PercheckLogin(string Loginname, string LoginPwd)
{
string str = "Server=(local);database=db_changfang;Uid=sa;Pwd=";
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("select count(*) from tb_PerUser where PerUName=@PerUName and PerPwd=@PerPwd", con);
cmd.Parameters.Add(new SqlParameter("@PerUName", SqlDbType.NVarChar, 20));
cmd.Parameters["@PerUName"].Value = Loginname;
cmd.Parameters.Add(new SqlParameter("@PerPwd", SqlDbType.NVarChar, 20));
cmd.Parameters["@PerPwd"].Value = LoginPwd;
cmd.Connection.Open();
int i = (int)cmd.ExecuteScalar();
cmd.Connection.Close();
return i;
}

判断i>0就显示有数据否则就插入数据
这是我一个登陆的代码,给你看下,自己研究吧。
grt 2008-07-09
  • 打赏
  • 举报
回复
6楼朋友:写详细点。

5楼朋友:MessageBox.Show("已经存在该数据");在WebForm中不能用,只能在WinForm中用。

4楼的朋友:我不太明白,请能注释一下。
江城老温 2008-07-09
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 grt 的回复:]
返回值怎么表示?
SqlDataSource SqlDataSource1 = new SqlDataSource();
SqlDataSource1.ConnectionString = "Data Source=SERVER2;Initial Catalog=anjz;Integrated Security=True";
SqlDataSource1.SelectCommand = "select count(*) from tableName whete id=10";
.......这里怎么写下去?


插入语句我会的,不用写了。
[/Quote]
接下来这么写就OK

SqlConnection conn = new SqlConnection(SqlDataSource1.ConnectionString);
conn.Open();
SqlCommand cmd = new SqlCommand(SqlDataSource1.SelectCommand.ToString());
SqlDataReader sdr = cmd.ExecuteReader();
if(sdr.Read())
//进行数据插入操作
else
MessageBox.Show("已经存在该数据");
我姓区不姓区 2008-07-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 ojlovecd 的回复:]
C# codeusing(SqlConnection conn=newSqlConnection("连接字符串"))
{
conn.Open();
SqlCommand cmd=newSqlCommand("select * from table where ID=10",conn);
SqlDataReader sdr=cmd.ExecuteReader();if(sdr.Read())//进行数据插入操作elseMessageBox.Show("已经存在该数据");
}
[/Quote]
写反了……

using(SqlConnection conn = new SqlConnection("连接字符串"))
{
conn.Open();
SqlCommand cmd = new SqlCommand("select * from table where ID=10",conn);
SqlDataReader sdr = cmd.ExecuteReader();
if(sdr.Read())
MessageBox.Show("已经存在该数据");
else
//进行数据插入操作
}

grt 2008-07-09
  • 打赏
  • 举报
回复
返回值怎么表示?
SqlDataSource SqlDataSource1 = new SqlDataSource();
SqlDataSource1.ConnectionString = "Data Source=SERVER2;Initial Catalog=anjz;Integrated Security=True";
SqlDataSource1.SelectCommand = "select count(*) from tableName whete id=10";
.......这里怎么写下去?


插入语句我会的,不用写了。
加载更多回复(10)

110,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧