2

有我用来将新数据传递到数据库(火鸟)的代码:

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);

也许您可以建议更好的算法,或者这很好?

4

2 回答 2

3

这种方式没问题,你使用的是一个做很多工作的命令生成器,你可以简单地将上面的代码翻译成插入命令,直接在数据库表上执行:

  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();

通过这种方式,您避免了命令生成器并将数据加载到数据表中,应该会更快......

于 2012-05-03T19:50:14.033 回答
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 ) );
    }
}
于 2012-05-03T19:51:47.120 回答