2

There is code that I use to pass new data to Database (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);

Maybe you can suggest better algorithm, or this is pretty good?

2 Answers 2

3

This way is OK, you are using a command builder that do a lot of work for, you can simply translate the above code into an insert command to execute directly on the database table :

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

In this way you avoid the command builder and loading the data into the Datatable, should be quite faster ...

Sign up to request clarification or add additional context in comments.

1 Comment

ok, addisional question, witch method is better to use if I want to check if data already exist in DB? in my example I know how to make that. How to make checking of existance in your ?
0

This is lighter for database:

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

For a select:

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

2 Comments

oh, thnx, is there any method to check if value exist in DB, for example name?
of course, you should use a sql select, if reader return rows it exists

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.