|
You last visited: Today at 06:57
Advertisement
[C#]need Help in MySql
Discussion on [C#]need Help in MySql within the .NET Languages forum part of the Coders Den category.
03/27/2012, 01:53
|
#1
|
elite*gold: 0
Join Date: Nov 2010
Posts: 238
Received Thanks: 218
|
[C#]need Help in MySql
hi all i need some help in mysql things in C# i done the connection part
by this but ineed some help in how to get data from database
PHP Code:
MySqlConnection conn = new MySqlConnection(); conn = new MySqlConnection(); conn.ConnectionString = "server=" + cbServerSelect.Text + ";" + "user id=" + txtUserInput.Text + ";" + "password=" + txtPassInput.Text + ";" + "database=my"; if (cbServerSelect.Text == "" || txtUserInput.Text == "" || txtPassInput.Text == "") { MessageBox.Show("All Fields must be entered"); } else { try { conn.Open(); MessageBox.Show("Connection Opened Successfully"); conn.Close(); } catch (Exception ex) { MessageBox.Show("Error Connecting to MySQL DB" + "\r\n" + "\r\n" + ex.Message); } }
i need some code like how to get data from tables and insert in it
so pleas i need some help
i get a code to get data but it make a problem when get names get it like this
the code
PHP Code:
MySqlDataReader Reader; MySqlCommand command = conn.CreateCommand(); command.CommandText = "select name from account"; conn.Open(); Reader = command.ExecuteReader(); while (Reader.Read()) { string thisrow = ""; for (int l = 0; l < Reader.FieldCount; l++) thisrow += Reader.GetValue(l) + ""; comboBox1.Items.Add(thisrow); listBox1.Items.Add(thisrow); } conn.Close();
Sorry For that because am not too good in Coding i learn every thing i know in it from internet
|
|
|
03/27/2012, 09:21
|
#2
|
elite*gold: 456
Join Date: Jan 2011
Posts: 18
Received Thanks: 4
|
To insert data:
Code:
//How I open the connection:
ConnectionString = "SERVER=" + parmarr[0] + ";" +
"DATABASE=" + parmarr[1] + ";" +
"UID=" + parmarr[2] + ";" +
"PASSWORD=" + parmarr[3] + ";";
MySqlConnection connection = new MySqlConnection(ConnectionString);
connection.Open();
MySqlCommand command = connection.CreateCommand();
//Here is the imortant part:
command.CommandText = "INSERT INTO mytable (column1,column2) VALUES ('test', 'test2')";
command.ExecuteNonQuery();
//----
//Close the connection:
command.Dispose();
connection.Close();
connection.Dispose();
And to get data:
Code:
ConnectionString = "SERVER=" + parmarr[0] + ";" +
"DATABASE=" + parmarr[1] + ";" +
"UID=" + parmarr[2] + ";" +
"PASSWORD=" + parmarr[3] + ";";
connection = new MySqlConnection(ConnectionString);
connection.Open();
command = connection.CreateCommand();
command.CommandText = "SELECT name FROM account;"
MySqlDataReader reader = command.ExecuteReader();
while(reader.Read())
{
//Whatever you want to do with the data...
string mystring = reader[0].ToString(); //I think this is what you need instead of Reader.GetValue()
}
reader.Close(); //Very important! You can't do another MySQL operation before you close the reader.
command.Dispose();
connection.Close();
connection.Dispose();
I haven't tested the code, but it should work...
I think the way you
|
|
|
03/27/2012, 18:10
|
#3
|
elite*gold: 0
Join Date: Nov 2010
Posts: 238
Received Thanks: 218
|
i will test it and see if it work any way work or no thanks for your time and for your help
regard wolfvb
PHP Code:
while (Reader.Read()) { // string to get the data in row string thisrow = ""; //this loop work on columns // in our example we have id,name,phone
string mystring = Reader[0].ToString(); //I think this is what you need instead of Reader.GetValue() thisrow += Reader.GetValue(0) + ""; // we add the data in single row to listbox comboBox1.Items.Add(thisrow); listBox1.Items.Add(thisrow); } reader.Close(); //Very important! You can't do another MySQL operation before you close the reader. command.Dispose(); connection.Close(); connection.Dispose();
its have same problem get names
in same way so any ideas its a privet server db Eo DB if this make any changes in codes
and there is a problem in this too
PHP Code:
MySqlConnection conn = new MySqlConnection(); conn = new MySqlConnection(); conn.ConnectionString = "server=" + cbServerSelect.Text + ";" + "user id=" + txtUserInput.Text + ";" + "password=" + txtPassInput.Text + ";" + "database=my";
MySqlCommand command = conn.CreateCommand(); //Here is the imortant part: command.CommandText = "UPDATE `cq_user` SET `name`='wolfvb[PM]' WHERE (`name`='[GM]~Ev!l~[PM]')"; command.ExecuteNonQuery(); //----
//Close the connection: command.Dispose(); conn.Close(); conn.Dispose();
the problem in here
PHP Code:
command.ExecuteNonQuery();
|
|
|
03/27/2012, 18:42
|
#4
|
elite*gold: 456
Join Date: Jan 2011
Posts: 18
Received Thanks: 4
|
Ok. You want to have all "name"s form the table "account" in a listbox. Right?
Try this:
Code:
command.CommandText = "SELECT name FROM account";
Reader = command.ExecuteReader();
while (Reader.Read())
{
listBox1.Items.Add(Reader[0].ToString());
}
I seriously don't know what you want to do with the for loop if you reade only one column from the database...
|
|
|
03/27/2012, 19:11
|
#5
|
elite*gold: 0
Join Date: Nov 2010
Posts: 238
Received Thanks: 218
|
**** the same problem i don't know why its get name like that
the insert part working good now with no problem the only problem here now
|
|
|
03/27/2012, 19:56
|
#6
|
elite*gold: 456
Join Date: Jan 2011
Posts: 18
Received Thanks: 4
|
Are you sure that the insert part work correctly? Please look with a tool like phpmyadmin or something else, if the data in the database is correct.
Maybe "System.Byte[]" is written in every Row of the table...
|
|
|
03/27/2012, 20:42
|
#7
|
elite*gold: 0
Join Date: Nov 2010
Posts: 238
Received Thanks: 218
|
i use Navicat to mage the db
and this is the table account and yeas the insert part work good i test it 3 times and work very good this a pic from table account
|
|
|
03/27/2012, 20:54
|
#8
|
elite*gold: 456
Join Date: Jan 2011
Posts: 18
Received Thanks: 4
|
You use the MySQL Connector Net Assemblie "MySql.Data.dll". Right?
Is "name" a varchar?
Try to read a integer from the db (for example "pointtime") [I think it's a a int...]
|
|
|
03/27/2012, 21:15
|
#9
|
elite*gold: 0
Join Date: Nov 2010
Posts: 238
Received Thanks: 218
|
its get numbers well with no problem so it get "id,etc.." well but in passsword and names this problem happen
|
|
|
03/27/2012, 21:50
|
#10
|
elite*gold: 456
Join Date: Jan 2011
Posts: 18
Received Thanks: 4
|
Do you create the table in the code? For example:
"CREATE TABLE IF NOT EXISTS user (id INT NOT NULL AUTO_INCREMENT, name VARCHAR (256), password VARCHAR (256), PRIMARY KEY (id));" ?
Could you post this string?
|
|
|
03/28/2012, 01:11
|
#11
|
elite*gold: 0
Join Date: Nov 2010
Posts: 238
Received Thanks: 218
|
there is the string
PHP Code:
CREATE TABLE `account` ( `id` int(4) unsigned zerofill NOT NULL auto_increment, `name` varchar(32) binary NOT NULL default '', `password` varchar(32) binary NOT NULL default '', `type` int(4) unsigned NOT NULL default '0', `point` int(4) default '0', `pointtime` int(4) unsigned zerofill NOT NULL default '20020318', `online` int(4) unsigned zerofill NOT NULL default '0000', `reg_date` datetime default NULL, `licence` int(4) unsigned default '0', `reg_flag` bigint(12) unsigned default '3', `netbar_ip` varchar(15) default '127.0.0.1', `ip_mask` varchar(15) default '255.255.255.255', `add_type` smallint(1) unsigned zerofill NOT NULL default '0', `VIP` int(4) unsigned NOT NULL default '3', `offline` int(4) default NULL, `pwd` varchar(32) default NULL, `idnum` varchar(20) default NULL, `email` varchar(32) default NULL, PRIMARY KEY (`id`), UNIQUE KEY `index_name_pass` (`name`,`password`), UNIQUE KEY `index_username` (`name`), KEY `index_type` (`type`), KEY `index_regtype` (`reg_flag`), KEY `index_isp` (`type`,`netbar_ip`), KEY `typereg` (`type`,`reg_date`) ) TYPE=MyISAM;
|
|
|
03/28/2012, 08:33
|
#12
|
elite*gold: 456
Join Date: Jan 2011
Posts: 18
Received Thanks: 4
|
Quote:
|
The BINARY and VARBINARY types are similar to CHAR and VARCHAR, except that they contain binary strings rather than nonbinary strings.
|
Try to convert the bytearray you get into a string.
listBox1.Items.Add(System.Text.ASCIIEncoding.GetSt ring(Reader[0])) or listBox1.Items.Add(System.Text.ASCIIEncoding.GetSt ring(Reader[0].ToString()))
(not sure which one) should do it...
If this doesn't work delte the word 'binary' in your CREATE TABLE string and create the table new.
|
|
|
 |
Similar Threads
|
[ERROR] Mysql can't connect to local Mysql server through socket
10/08/2011 - Metin2 Private Server - 3 Replies
Hallo Liebe Com,
Undzwar habe ich ein Problem mit Mysql.
Als ich Mysql installiert habe konnte ich Problemlos drauf connecten,auch mit navicat.Aber als ich die Serverfiles (2011er) installiert habe und rebootet habe ging aufeinmal mysql nichtmehr.Wenn ich mysql -p eingebe steht dann da:
Wenn ich den Startbefehl eingebe steht da:
Was kann das sein? Und es passiert immer nur nach der Serverfiles installation.
Ich habe den Server schon 3x resetten lassen und es passierte immer das...
|
[MYSQL dont Start]su: unknown login: mysql
05/08/2011 - Metin2 Private Server - 2 Replies
Moin Leute und zwar hab ich ein Problem das ich bis heute noch nie hatte,
Ich hab mein Root Server neu gestartet und dabei bemerkt das der Mysql Server nicht wieder mit hoch gefahren ist.....
Dann hab ich versucht per: ihn wieder zu starten aber es gibt mir nur das hier aus:
Die Frage ist jetzt an was liegt das?
Danke schonmal.
|
[ERROR] Mysql can't connect to local Mysql server through socket
11/06/2010 - Metin2 Private Server - 5 Replies
I just recently tried to configure a DNS server for MT2.. It came up with this error
ERROR: 2002 (HY000): Can't connect to local Mysql server through socket '/tmp/mysql.sock' (2)
Do I need to do a fresh installation of FBSD?
:S
Thanks.
|
MySQL Navicat 1130-Host'5.xxx.xx.xxx' is not allowed to connect to the MySQL Server
08/07/2010 - Metin2 Private Server - 14 Replies
Hallo com,
ich habe ein Problem mit Navicat. Undzwar habe ich diesen Fehler hier : "1130-Host'5.xxx.xx.xxx' is not allowed to connect to the MySQL Server" seid gestern.
Ich dachte mir mal ich änder mein Navicat Passwort um... Als ich dies getan habe, und meinen Server rebootet habe und Navicat neugestartet habe, und ich mich wieder in Navicat einloggen wollte kam diese Fehlermeldung. Nun habe ich das Problem das ich mich nicht mehr mit Navicat connecten kann. Habe schon alles versucht...
|
All times are GMT +1. The time now is 06:59.
|
|