2013-06-12 140 views
0

我尝试更新sqldatabase服务器。我之前在visual studio中使用buildin“Local Database”做了这个。乙 我这样做(简称)C#SQL Server更新

System.Data.SqlServerCe.SqlCeCommandBuilder cb; 
cb = new System.Data.SqlServerCe.SqlCeCommandBuilder(da); 
cb.DataAdapter.Update(ds1, "Alarmen"); 

但内置的数据库没有达到要求。我不得不使用Microsoft SQL Server 2005. 我也尝试过这样做,但那不起作用。 首先我填一个数据库中使用以下命令设置数据库:\

fillDataSet() 
{ 
    SqlConnection _con = new SqlConnection("server=(local)\\SQLExpress;database=Alarmen;integrated Security=SSPI;"); 
    string sql = "SELECT * FROM tbl_alarmen"; 

    try 
    { 
     _con.Open(); 
    } 
    catch (Exception ex) 
    { 
     log.Info("Database kon niet geopend worden " + ex); 
    } 

    DataSet ds1 = new DataSet(); 

    try 
    { 
     sql = "SELECT * FROM dbo.tbl_alarmen"; 

     _cmd = new SqlCommand(sql, _con); 
     _dap = new SqlDataAdapter(_cmd); 

     _dap.Fill(ds1, "Alarmen"); 

     foreach (DataRow drow in ds1.Tables["Alarmen"].Rows) 
     { 
      log.Info("Test"); 
     } 

    } 
    catch (Exception err) 
    { 
     log.Info("Database lezen mislukt " + err); 
    } 

    try 
    { 
     _con.Close(); 
    } 
    catch (Exception ex) 
    { 
     log.Info("Database sluiten mislukt " + ex); 
    } 
     _dap.Fill(ds1, "Alarmen"); 

     foreach (DataRow drow in ds1.Tables["Alarmen"].Rows) 
     { 
      log.Info("Test"); 
     } 

    } 
    catch (Exception err) 
    { 
     log.Info("Database lezen mislukt " + err); 
    } 

    try 
    { 
     _con.Close(); 
    } 
    catch (Exception ex) 
    { 
     log.Info("Database sluiten mislukt " + ex); 
    } 
} 

然后我添加行和修改数据集中的项目,并尝试OP更新数据库的变化。

UpdateDatabase() 
{ 
    SqlConnection _con = new SqlConnection("server=(local)\\SQLExpress;database=Alarmen;integrated Security=SSPI;"); 
    string sql = "SELECT * FROM tbl_alarmen"; 

    try 
    { 
     _con.Open(); 
    } 
    catch (Exception ex) 
    { 
     log.Info("Database kon niet geopend worden " + ex); 
    } 

    try 
    { 
     var con = new SqlConnection(connectionString); 
     var adapter = new SqlDataAdapter("SELECT * FROM tbl_alarmen", con); 
     new SqlCommandBuilder(adapter); 

     // 
     // Fill the DataAdapter with the values in the DataTable. 
     // 
adapter.Fill(ds1); 
     // 
     // Insert the data table into the SQL database. 
     // 
adapter.Update(ds1);    
    } 
    catch (Exception e) 
    { 
     log.Info("Fill database failed " + e); 
    } 

    try 
    { 
     _con.Close(); 
    } 
    catch (Exception ex) 
    { 
     log.Info("Database sluiten mislukt " + ex); 
    } 
} 

我没有收到任何错误,但数据库一直为空。

我现在不应该使用“SELECT *”来解决安全问题。我需要在将来改变这一点。

我看到我没有问一个真正的问题。我在代码中做了什么错误,我该如何修复它。

+1

好了,第一反模式我在代码中看到的是,打开时如果有异常抛出连接,你报告错误,然后**继续**尝试使用连接。 –

+0

感谢您的意见。这是我可以使用的东西。但我使用try-catch主体来查看是否有错误。问题是错误不是trown。所以我认为他们不会给出任何错误。 – Gulpener

+1

另外,考虑在打开一个'SqlConnection'时考虑使用'using'子句,以便在发生问题时更轻松地处理连接。看到这个[教程](http://www.dotnetperls.com/sqlconnection)。 此外,您使用'try/catch'块只记录异常信息而不做其他事情。这不是一个好的做法 - 只有在你打算做一些事情(例如回滚)时才会发现异常。更好,更干净的方法是使用'AppDomain.UnhandledException'。例如:[link](http://stackoverflow.com/questions/3133199/net-global-exception-handler-in-console-application)。 – w128

回答

0

尝试

adapter.Fill(ds1, "Alarmen") 

然后

adapter.Update(ds1, "Alarmen"); 

this MS link for more info

+0

非常感谢您的回答。现在我还有其他错误。但我认为这与我的问题无关。 – Gulpener