Home > Net >  C # how to connect only one database, and then perform three query SQL statement, and then take to t
C # how to connect only one database, and then perform three query SQL statement, and then take to t

Time:03-27


I have a database with a total of 30 fields, field called id, field 2, 3, field... Field 30, I want to connect only one database, then in the database query in turn these three kinds of qualified record, find out the record of field 2, field 3,... 30 numerical fields,

1, find the id="xxxxxx01" records, the field 2, the field 3,... Field of 30 numerical ordinal,
2, find the id="xxxxxx02" records, the field 2, the field 3,... Field of 30 numerical ordinal,
3, find the id="xxxxxx04" records, the field 2, the field 3,... Field of 30 numerical ordinal,

here is my code, but there is a problem, please help to correct me,

 
Try
{
MySqlConnection COMconn=null;
String COMconStr="server=localhost; Port=3306; User Id=root; Password=winelec68331; The database=cableid; Allow the User Variables=True ";//the connection string
COMconn=new MySqlConnection (COMconStr);
COMconn. The Open ();

//query id="xxxxxx01" records, the field 2, the field 3,... 30 numerical fields in turn out
String COM1sql="select * from table1 where id='" +" xxxxxx01 "+"' ";
MySqlCommand COM1Cmd=new MySqlCommand (COM1sql COMconn);
MySqlDataReader COM1db=COM1Cmd. ExecuteReader ();
If (COM1db. Read ())
{
Parameters_buffer [0]=the Convert. ToInt32 (COM1db [] "field 2");//- & gt; Remove the field values
2Parameters_buffer [1]=the Convert. ToInt32 (COM1db/" field 3 ");//- & gt; Take out the field of 3 numerical
}

//query id="xxxxxx02" records, the field 2, the field 3,... 30 numerical fields in turn out
String COM2sql="select * from table1 where id='" +" xxxxxx02 "+"' ";
MySqlCommand COM2Cmd=new MySqlCommand (COM2sql COMconn);
MySqlDataReader COM2db=COM2Cmd. ExecuteReader ();
If (COM2db. Read ())
{
Parameters_buffer [2]=the Convert. ToInt32 (COM1db [] "field 2");//- & gt; Remove the field values
2Parameters_buffer [3]=the Convert. ToInt32 (COM1db/" field 3 ");//- & gt; Take out the field of 3 numerical
}

//query id="xxxxxx03" records, the field 2, the field 3,... 30 numerical fields in turn out
String COM3sql="select * from table1 where id='" +" xxxxxx03 "+"' ";
MySqlCommand COM3Cmd=new MySqlCommand (COM3sql COMconn);
MySqlDataReader COM3db=COM3Cmd. ExecuteReader ();
If (COM3db. Read ())
{
Parameters_buffer [4]=the Convert. ToInt32 (COM1db [] "field 2");//- & gt; Remove the field values
2Parameters_buffer [5]=the Convert. ToInt32 (COM1db/" field 3 ");//- & gt; Take out the field of 3 numerical
}
}
The catch (Exception ex)
{
MessageBox. Show (ex. Message);//output error message
}
The finally
{
COMconn. Close ();
}

CodePudding user response:

If instead the while

CodePudding user response:

reference 1st floor jhonsonzhang response:
if instead while


OK, find the id="xxxxxx01" records but find id="xxxxxx02" records, the system reports: database open or connection has a problem,

CodePudding user response:

You now is even a data, COMconn Open only one, want to use an SQL statement to achieve query?
Method (not recommended) :
Select * from table1 where id=1
The union
Select * from table1 where id=2
The union
Select * from table1 where id=3

Method 2:
Select * from table1
A statement of the data found out, in a DataTable, called dt1, then by using the approach of the DataTable Select query data
DRS DataRow []=dt1. Select (" id=1 ")
If DRS. The Length is not zero, the result of the query to the operator, you can remove it will be OK

CodePudding user response:

refer to the second floor ba_wang_mao response:
Quote: refer to 1st floor jhonsonzhang response:
if instead while


OK, find the id="xxxxxx01" records but find id="xxxxxx02" records, the system reports: database open or connection has a problem,


Behind you code with reference to the first change, change the sqlcommand SQL statements, and then went on reading it,

CodePudding user response:

Don't use three sqlcommand

CodePudding user response:

reference 3 floor heart blue response: 168
you ah, now it is even a data COMconn Open only one, want to use an SQL statement to achieve query?
Method (not recommended) :
Select * from table1 where id=1
The union
Select * from table1 where id=2
The union
Select * from table1 where id=3

Method 2:
Select * from table1
A statement of the data found out, in a DataTable, called dt1, then by using the approach of the DataTable Select query data
DRS DataRow []=dt1. Select (" id=1 ")
If DRS. The Length is not zero, the result of the query to the operator, you will again it will be OK



I want to use 3 SQL statements for the query,
The first SQL statements, the query id="xxxxxx01" records, then remove the record field 2, field 3,... , 30 numerical fields,
Article 2 SQL statements to query id="xxxxxx02" records, then remove the record field 2, field 3,... , 30 numerical fields,
Article 3 SQL statements to query id="xxxxxx03" records, then remove the record field 2, field 3,... , 30 numerical fields,

CodePudding user response:

Directly select * from table1 where id IN (' xxxxxx01 ', 'xxxxxx02', 'xxxxxx03) had not
Then
 
While (db. The Read ())
{
String id=the Convert. ToString (db (" id "));

The switch (id)
{
Case "xxxxxx01" :
//related operations
break;
Case "xxxxxx02" :
//related operations
break;
Case "xxxxxx03" :
//related operations
break;
}
}

CodePudding user response:

Directly select * from table1; Select * from table2. Select * from table3.
With the dataset to save the dataset [0], the dataset [1], the dataset [2]

CodePudding user response:

refer to the eighth floor YBcsdn1996 response:
directly select * from table1; nullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related