c#将数据添加到firebird数据库

时间:2012-05-03 19:42:25

标签: c# database firebird

我使用代码将新数据传递给数据库(firebird):

FbConnection fbCon = new FbConnection(csb.ToString());
FbDataAdapter fbDAdapter = new FbDataAdapter("SELECT ID,name,score FROM players",fbCon);
FbCommandBuilder Cmd = new FbCommandBuilder(fbDAdapter);
DataSet DSet = new DataSet();
fbDAdapter.Fill(DSet);
DataRow rw = DSet.Tables[0].NewRow();
rw["ID"] = zID + 1;
rw["name"] = var;
rw["score"] = score;
DSet.Tables[0].Rows.Add(rw);
fbDAdapter.Update(DSet);

也许你可以建议更好的算法,或者这是非常好的?

2 个答案:

答案 0 :(得分:2)

这种方式没关系,你使用的命令构建器做了很多工作,你可以简单地将上面的代码翻译成insert命令直接在数据库表上执行:

  FbConnection fbCon = new FbConnection(csb.ToString());
  FbCommand fbCom = new FbCommand("INSERT INTO players(ID,name,score) VALUES (@id,@name,@score)", fbCon);
  fbCom.Parameters.AddWithValue("id", zID + 1);
  fbCom.Parameters.AddWithValue("name", var);
  fbCom.Parameters.AddWithValue("score", score);
  fbCom.ExecuteNonQuery();

通过这种方式,您可以避免命令构建器并将数据加载到Datatable中,应该更快......

答案 1 :(得分:0)

这对数据库来说更轻松:

      FbConnectionStringBuilder csb = new FbConnectionStringBuilder( );
      csb.ServerType = FbServerType.Default;
      csb.Database = Settings.Default.LastBDPath;
      csb.Password = Settings.Default.LastBDPassword; //            "masterkey";
      csb.UserID = Settings.Default.LastBDUser;       //            "SYSDBA";
      connection = new FbConnection( csb.ToString( ) );

      connection.Open( ); 

      string scriptLine = string.Format("INSERT INTO TABLE (Id, Name, Score) values ({0}, '{1}', {2}", zId+1, var, score) ; 
      FBCommand command = new FbCommand( scriptline, connection );
      command.ExecuteNonQuery( );

对于选择:

string sql = string.Format("select * from table where name='{0}';", your_name);

FbCommand command = new FbCommand( sql ), connection );
FbDataReader reader = command.ExecuteReader( );
while ( reader.Read( ) )
{
    for ( int i=0; i<reader.FieldCount; i++ )
    {
         objects[i].Add( reader.GetValue( i ) );
    }
}