2011-12-29 112 views
-4

我在RichTextBox的前一個列表:插入名單數據庫

dogs 
cats 
books 
other 

如何在表中「動物」插入列「家」之一行?

回答

1

事情是這樣的使用ODBC

string connectionString ="yourConnectionString" 
using (DbConnection conn = new OdbcConnection(connectionString)) 
using (DbCommand cmd = conn.CreateCommand()) 
{ 
    cmd.CommandText = "INSERT INTO Animals(home) VALUES (@animal)"; 
    DbParameter p = cmd.CreateParameter(); 
    p.ParameterName = "@animal"; 
    cmd.CommandType = CommandType.Text; 
    conn.Open(); 
    using (DbTransaction tran = conn.BeginTransaction()) 
{ 
    cmd.Transaction = tran; 
    try 
    { 
     foreach (string line in MyRichTextBox.Lines) 
     { 
      p.Value = line; 
      cmd.ExecuteNonQuery(); 
     } 
     tran.Commit(); 
     conn.Close(); 
    } 
    catch (Exception e) 
    { 
     tran.Rollback(); 
     throw(e); 
    } 
} 

}

+0

請給提問者一個機會,找出自己的答案自己。 – 2011-12-29 00:30:42

+0

感謝您的回答,但此代碼返回錯誤 – jolly 2011-12-29 01:27:44

+0

@jolly - 那麼您希望有人給你一個有效的代碼示例,而無需自己做任何工作?讓我感到驚訝。 – 2011-12-29 01:40:18