本文介绍了如何检查插入数据库中的值在C#中是唯一的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在C#窗口应用程序中工作.我想知道如何检查UniqueID字段是否唯一,并且没有插入重复的数据.我的代码是...

I have work in C# window application . I want to know how to check whether the UniqueID field is unique and no duplicate data is inserted . my code is...

 

 

DatabaseInfo数据库= new DatabaseInfo();
                    database.DatabaseName ="ALPHA";
                    database.ServerDsn ="DELLVOSTRO";
                    database.DbType = DatabaseType.Microsoft;
               
                   


                    AlphaDatabaseManager alphadatabasemanager =新的AlphaDatabaseManager(数据库);
                    SqlConnection conn =新的SqlConnection(数据源= DELLVOSTRO;初始目录= ALPHA;集成安全性= True");
                    DataSet localDataSet1 =新的DataSet();
          
            
                   字符串sTable =插入COUNTRY(UNIQUEID,ISO_CODE,ALPHA_CODE,NUMERIC_CODE,NAME,CONTINENT)值('''
               b + ultraNumericEditorUniqueID.Text +','" + ultraMaskedEditIsoCode.Text +','" + ultraMaskedEditAlphaCode.Text +``','''
               b + ultraNumericEditorNumericCode.Text +``','''; + ultraMaskedEditName.Text +``','''; + ultraComboEditorContinent.Text +``')'';
                    SqlTransaction转换;
                    alphadatabasemanager.CreateConnectionObject();
                   试试
                    {
                    alphadatabasemanager.OpenConnection(conn);
                    transation = conn.BeginTransaction();
                    alphadatabasemanager.ExecuteNonQueryCommand(sTable,conn,transation);
                    alphadatabasemanager.CommitTransaction(transation);

}

  catch(异常例外)
           {
                             MessageBox.Show(ex.Message);
           }
       }

DatabaseInfo database = new DatabaseInfo();
                    database.DatabaseName = "ALPHA";
                    database.ServerDsn = "DELLVOSTRO";
                    database.DbType = DatabaseType.Microsoft;
                  
                   


                    AlphaDatabaseManager alphadatabasemanager = new AlphaDatabaseManager(database);
                    SqlConnection conn = new SqlConnection("Data Source = DELLVOSTRO; Initial Catalog = ALPHA; Integrated Security = True");
                    DataSet localDataSet1 = new DataSet();
           
              
                    string sTable = "Insert Into COUNTRY (UNIQUEID , ISO_CODE , ALPHA_CODE , NUMERIC_CODE , NAME , CONTINENT) values('"
                        + ultraNumericEditorUniqueID.Text + "','" + ultraMaskedEditIsoCode.Text + "','" + ultraMaskedEditAlphaCode.Text + "','"
                        + ultraNumericEditorNumericCode.Text + "','" + ultraMaskedEditName.Text + "','" + ultraComboEditorContinent.Text + "')";
                    SqlTransaction transation ;
                    alphadatabasemanager.CreateConnectionObject();
                    try
                    {
                    alphadatabasemanager.OpenConnection(conn);
                    transation = conn.BeginTransaction();
                    alphadatabasemanager.ExecuteNonQueryCommand(sTable, conn, transation);
                    alphadatabasemanager.CommitTransaction(transation);

}

 catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

推荐答案

 

简单的方法是在数据库端创建函数/过程并使用参数调用一个.作为回报,我们有唯一的ID

simply approach is to create function/procedure on database side and call one with parameters. in return we've got unique id

change Insert (ISO_CODE , ALPHA_CODE , NUMERIC_CODE , NAME , CONTINENT)...

into

DB function 
int tryToInsert(ISO_CODE , ALPHA_CODE , NUMERIC_CODE , NAME , CONTINENT) {}

如果您想将id传递给查询,则可以创建新函数以返回新的唯一id,并尝试插入,只要成功成功就可以插入:

if you want to pass id into query, maybe create new function to return new unique id and try to insert as long as it happend with success:

 

long

 id = DB.GetUniqueID();

while

 (DB.InsertData(id, ...) == 0)
{
 id = DB.GetUniqueID();
}


这篇关于如何检查插入数据库中的值在C#中是唯一的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 18:07