当前位置:编程学习 > C#/ASP.NET >>

手动插入自增列

[csharp] 
//测试插入自增列 
               string sqlstr = "SET IDENTITY_INSERT t1 ON;"; 
               lists.Add(sqlstr); 
               sqlstr = "INSERT INTO t1       (id, name) VALUES ("+i.ToString()+", '" + i.ToString()+"');"; 
               lists.Add(sqlstr); 
               sqlstr = "set identity_insert t1 off;"; 
               lists.Add(sqlstr); 
 
               da.SqlCeExecuteNonQuery(lists); 

[csharp]
public void SqlCeExecuteNonQuery(List<string> lists) 
       { 
           SqlCeConnection conn = new SqlCeConnection(); 
           SqlCeCommand comm = null; 
           try 
           { 
               conn.ConnectionString = PDAConnectionLocStr; 
               conn.Open(); 
               comm = new SqlCeCommand("", conn); 
               for (int i = 0; i < lists.Count; i++) 
               { 
                   comm.CommandText = lists[i]; 
                   comm.ExecuteNonQuery(); 
               } 
 
 
 
           } 
           catch (SqlCeException ex) 
           { 
 
           } 
           finally 
           { 
               conn.Close(); 
               conn.Dispose(); 
               comm.Dispose(); 
           } 
       } 

 

注意:自增列没有主键 手动插入可能发生ID号重复的情况

补充:软件开发 , C# ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,