| |

VerySource

 Forgot password?
 Register
Search
View: 886|Reply: 8

This code for operating the database is wrong.

[Copy link]

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

Post time: 2020-3-6 20:30:02
| Show all posts |Read mode
The error message is:
For SelectCommand that does not return any key column information, dynamic SQL generation of UpdateCommand is not supported.

public partial class _Default: System.Web.UI.Page
{
    private OleDbDataAdapter DA = new OleDbDataAdapter ();
    protected void Page_Load (object sender, EventArgs e)
    {
WriteData ();
    }

    private DataTable GetData ()
    {
        DataTable Dt = new DataTable ();
        OleDbConnection Con = new OleDbConnection (@ "constr");
        DA.SelectCommand = new OleDbCommand ("select [name], [new], [buy]", Con); // There is no primary key selected because this table does not have a primary key, and I cannot add a primary key
        DA.Fill (Dt);
        Dt.PrimaryKey = new DataColumn [] {Dt.Columns ["id"]}; // Create a primary key
        return Dt;
    }

    private void WriteData (DataTable Dt)
    {
        OleDbConnection Con = new OleDbConnection (@ "constr");
        DA.InsertCommand = new OleDbCommand ("insert sql", Con);
        OleDbCommandBuilder Scb = new OleDbCommandBuilder (DA);
        DA.Update (GetData ());
    }
}
Reply

Use magic Report

0

Threads

1

Posts

0.00

Credits

Newbie

Rank: 1

Credits
0.00

 China

Post time: 2020-3-6 20:33:58
| Show all posts
Niu Niu. . . . . .
Reply

Use magic Report

0

Threads

8

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-5-25 06:45:01
| Show all posts
You are inserting a piece of data, but you don't need to have a primary key. Will it be better to insert directly?
Reply

Use magic Report

0

Threads

119

Posts

67.00

Credits

Newbie

Rank: 1

Credits
67.00

 China

Post time: 2020-5-26 20:30:01
| Show all posts
Write the corresponding SQL statement yourself:
Probably as follows:
In fact, you still write sentences yourself, because there are a lot of garbage generated automatically, for example,
  // sqlInsertCommand1
  //
  this.sqlInsertCommand1.CommandText = "INSERT INTO student (stuno, name) VALUES (@stuno, @name)";
  this.sqlInsertCommand1.Connection = this.conn;
  this.sqlInsertCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ("@ stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));
  this.sqlInsertCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ("@ name", System.Data.SqlDbType.VarChar, 50, "name"));
  //
  // sqlUpdateCommand1
  //
  this.sqlUpdateCommand1.CommandText = "UPDATE student SET stuno = @stuno, name = @name WHERE (stuno = @Original_stuno)";
  this.sqlUpdateCommand1.Connection = this.conn;
  this.sqlUpdateCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ("@ stuno", System.Data.SqlDbType.VarChar, 4, "stuno"));
  this.sqlUpdateCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ("@ name", System.Data.SqlDbType.VarChar, 50, "name"));
  this.sqlUpdateCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ("@ Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte) (0 )), ((System.Byte) (0)), "stuno", System.Data.DataRowVersion.Original, null));
      
  // sqlDeleteCommand1
  //
  this.sqlDeleteCommand1.CommandText = "DELETE FROM student WHERE (stuno = @Original_stuno)";
  this.sqlDeleteCommand1.Connection = this.conn;
  this.sqlDeleteCommand1.Parameters.Add (new System.Data.SqlClient.SqlParameter ("@ Original_stuno", System.Data.SqlDbType.VarChar, 4, System.Data.ParameterDirection.Input, false, ((System.Byte) (0 )), ((System.Byte) (0)), "stuno", System.Data.DataRowVersion.Original, null));
    
  this.sqlDa.DeleteCommand = this.sqlDeleteCommand1;
  this.sqlDa.InsertCommand = this.sqlInsertCommand1;
  this.sqlDa.UpdateCommand = this.sqlUpdateCommand1;
  try
  {
  sqlDa.Update (dt.GetChanges, "student");
  return true;
  }
  catch (System.Data.SqlClient.SqlException ex)
  {
    
  return false;
  }
  finally
  {
  conn.Close ();
  }
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-5-28 20:45:01
| Show all posts
I don't want to add update command. . . .
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-5-28 22:45:02
| Show all posts
private DataTable GetData ()
    {
        DataTable Dt = new DataTable ();
        DataColumn pk = new DataColumn ("id");
        pk.AutoIncrement = true;
        Dt.Columns.Add (pk);
        Dt.PrimaryKey = new DataColumn [] {Dt.Columns ["id"]};
        OleDbConnection SourceCon = new OleDbConnection (@ "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = D:\qh (1); Extended Properties = dBASE IV;");
        string SourceSql = "select * from [all] where ABS (new)> 0";
        DA.SelectCommand = new OleDbCommand (SourceSql, SourceCon);
        DA.Fill (Dt);
        return Dt;
    }


I still set the primary key or not
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-7-22 17:00:01
| Show all posts
The original database is not in my hands and cannot add a primary key. What should I do?
Reply

Use magic Report

0

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-7-23 00:45:01
| Show all posts
You used update, but there is no corresponding updatecommand
Reply

Use magic Report

1

Threads

5

Posts

5.00

Credits

Newbie

Rank: 1

Credits
5.00

 China

 Author| Post time: 2020-7-23 09:30:01
| Show all posts
I have two databases and I want to copy all the data from A database to B database

So after I use the oledb adapter to get all the data of A, I want to use this adapter to insert into the database B at one time. It shouldn’t need to be updated!
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list