select id from table where id=rid

解决方案 »

  1.   

    先查询数据库select id from table where id=rid
    判断查询是否为空,若为空,就插入insert into table(id) values ('rid')
      

  2.   

    表示伪随机数生成器,一种能够产生满足某些随机性统计要求的数字序列的设备。有关此类型所有成员的列表,请参阅 Random 成员。
      

  3.   

    int test;
    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的數全部存在的情況,如果全部存在以上是死循環
      

  4.   

    不知道和你的意思是否一样,你可以把数据库连接信息改成你的数据库,然后新建一张简单的表测试一下。如可以在tempdb里新建一张tableA的表,里面有两个字段就可以了,一个id,一个othersusing System;
    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();

    }
    }
    }