select id from table where id=rid
解决方案 »
- WebBrowser 显示和IE不一致,急救。
- 弱弱问句,你们编程都有做UML?在你们编程中UML非常重要?有没有案例推荐的?
- 文件上传数据库问题
- C# ACCESS 求和语句出错
- 如何在一台电脑上播放ppt文件另一台电脑上可以也播放
- 分不多,但是准给分,疑问WA问题
- 如何使用CheckBox来删除DataGrid中的某些行(WEB),谢谢!在线等待
- 最近在学c#,也是根据别人的教程边学边做的,一个密码管理器,大家看看哦
- 一個關于事件定義類的小錯誤,查不出來.
- 如何利用C#打开一个对话框获取文件夹,就象新建项目时选择文件夹一样?
- 如何在关闭应用程序后,该进程继续运行!
- 如何得到新添加的记录编号?
判断查询是否为空,若为空,就插入insert into table(id) values ('rid')
while(true)
{
Random rd=new Random();
int iTemp=rd.Next(100)
SQL="Select count(*) from Table where id="+iTemp.ToString();
///Query DataBase,iRows is rows count
if(iRows==0)
{
test=iTemp
break;
}
}
以上沒有考慮如果數據庫中0-100的數全部存在的情況,如果全部存在以上是死循環
using System.Data;
using System.Data.SqlClient;namespace ConsoleApplication1
{
class ExecuteSql
{
private string Conn;
public ExecuteSql(string conn)
{
Conn = conn;
}
public bool IsExist(int id)
{
SqlConnection myConn = new SqlConnection(Conn);
myConn.Open();
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConn;
myCommand.CommandText = "select * from tableA where ID = '"+id+"'";
myCommand.CommandType = CommandType.Text;
if( myCommand.ExecuteScalar() == null || myCommand.ExecuteScalar().ToString() == "")
{
return false;
}
else
{
myConn.Close();
Console.WriteLine("数据{0}已经存在",id);
return true;
}
}
public void ExecuteInsert(int id)
{
SqlConnection myConn = new SqlConnection(Conn);
myConn.Open();
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConn;
myCommand.CommandText = "insert into tableA values ('"+id+"','asdf')";
myCommand.ExecuteNonQuery();
myConn.Close();
Console.WriteLine("数据{0}已经插入",id);
} }
class App
{
private static void test()
{
System.Random randomID = new System.Random();
int tmpID = randomID.Next(1,101);
ExecuteSql executeSql = new ExecuteSql("数据库连接信息");
if(executeSql.IsExist(tmpID))
return;
else
executeSql.ExecuteInsert(tmpID);
}
static void Main(string[] args)
{
test();
Console.Read();
}
}
}