Home > Net >  C # ASP.NET a MYSQL database named a long field, and c # reserved word name repetition, how to use t
C # ASP.NET a MYSQL database named a long field, and c # reserved word name repetition, how to use t

Time:10-09


1, colleagues defines a database, which contains a field named long, and c # reserved word moniker
2, I define a textBox4 controls, in the picture and 1 button button, want to click a button, to insert data into a textBox4 numerical control,


 int mlong=the Convert. ToInt32 (TextBox4. Text); 



String ConnectionStr="server=localhost; Port=3306; User Id=root; Password=12345678; The database=cableid; Allow the User Variables=True ";
MySqlConnection Connection=new MySqlConnection (ConnectionStr);
MySqlCommand command=new MySqlCommand ();
Command. The Connection=Connection;

Try
{
Connection. The Open ();
MySqlCommand CMD=new MySqlCommand (" insert into table1 set mlong=@ long, "the Connection).
CMD. The Parameters. AddWithValue (" @ long, "mlong);
If (CMD) ExecuteNonQuery () & gt; 0)
{
Response. Write (" & lt; script> Alert (' add success) & lt;/script>" );
}
The else
{
Response. Write (" & lt; script> Alert (' add failure) & lt;/script>" );
}
}
The catch (Exception ex)
{
Response. Write (" & lt; script> Alert (ex. Message) & lt;/script>" );
}



Now exists the question is: not cut into the database, there is no any response,

CodePudding user response:

, defining a field called long, the field name and c # reserved word long name repetition,
in the following way
 int mlong=the Convert. ToInt32 (TextBox4. Text); 

MySqlCommand CMD=new MySqlCommand (" insert into table1 set mlong=@ long, "the Connection).
CMD. The Parameters. AddWithValue (" @ long, "mlong);
CMD. ExecuteNonQuery ();


Cannot be mlong inserted into a database,




Two, but as long as the field name in the database is modified to mlong,
in the following way
Int mlong=the Convert. ToInt32 (TextBox4. Text);

MySqlCommand CMD=new MySqlCommand (" insert into table1 set mlong=@ mlong, ", Connection).
CMD. The Parameters. AddWithValue (" @ mlong, "mlong);
CMD. ExecuteNonQuery (); [/code]

Mlong can be inserted into the database,


CodePudding user response:

Keyword name repetition, can add the at symbol

CodePudding user response:

refer to the second floor of guiyang horse Ma Shanfu plugging waterproof engineering professional maintenance of swimming pool response:
keyword name repetition, can add the at symbol


The field name no keyword name repetition and MySQL, but a and c # keyword zhangs!

CodePudding user response:

 public async Task InsertRecord (string strDBName, string strTableName, string strInsertRecord)//to add 
{
ExecutionResult er=new ExecutionResult ();
Return await Task The Run (()=& gt;
{
Try
{
String strSelectDB=$" USE {strDBName} ";
MySqlCommand cmdSelectDB=new MySqlCommand (strSelectDB, conn);
Conn. The Open ();
CmdSelectDB. ExecuteNonQuery ();
MySqlCommand CMD=new MySqlCommand (strInsertRecord, conn);

CMD. ExecuteNonQuery ();
Conn. Close ();
Er. The Status=true;
Er. Message=$" insert {strDBName} - & gt; {strTableName} successfully ";
}
The catch (Exception e)
{
Er. The Status=false;
Er. Message=$" insert {strDBName} - & gt; Fail: {strTableName} {e. oString ()} ";
}

Return the er;
});//add a record
}

CodePudding user response:

reference 4 floor Antony_WU_SZ response:
 public async Task InsertRecord (string strDBName, string strTableName, string strInsertRecord)//to add 
{
ExecutionResult er=new ExecutionResult ();
Return await Task The Run (()=& gt;
{
Try
{
String strSelectDB=$" USE {strDBName} ";
MySqlCommand cmdSelectDB=new MySqlCommand (strSelectDB, conn);
Conn. The Open ();
CmdSelectDB. ExecuteNonQuery ();
MySqlCommand CMD=new MySqlCommand (strInsertRecord, conn);

CMD. ExecuteNonQuery ();
Conn. Close ();
Er. The Status=true;
Er. Message=$" insert {strDBName} - & gt; {strTableName} successfully ";
}
The catch (Exception e)
{
Er. The Status=false;
Er. Message=$" insert {strDBName} - & gt; Fail: {strTableName} {e. oString ()} ";
}

Return the er;
});//add a record
}



Private MySqlConnection conn=new MySqlConnection (" Data Source=localhost; Persist Security Info=yes; UserId=root; The PWD="Password," ");

https://blog.csdn.net/ericwuhk/article/details/106411528
  •  Tags:  
  • C#
  • Related