当我尝试连接到mysql数据库时,以semicoloun获取错误 [英] Getting error at semicoloun when I tried to connect to mysql database

查看:81
本文介绍了当我尝试连接到mysql数据库时,以semicoloun获取错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我写的代码





private:System :: Void button1_Click(系统:: Object ^ sender,System :: EventArgs ^ e){

String ^ constring = Ldatasourse = localhost; port = 3306; username = root; password = password;



MySqlConnection ^ conDataBase = gcnew MySqlConnection(constring); < ;-(它显示带有红线的分号,Argument Exception未处理的问题状态,不支持关键字,参数名称:datasource)



MySqlCommand ^ cmdDataBase = gcnew MySqlCommand(select * from login_acc.login where Username ='+ this-> textBox1 + '和密码='+ this-> textBox2->文字+';,conDataBase);

MySqlDataReader ^ myReader;

try {

conDataBase-> Open();

myReader = cmdDataBase-> ExecuteReader();

int count = 0;

while(myReader-> Read()){

count = count + 1;



}

if(count == 1){

MessageBox :: Show(Login!);

}

else if(count> 1){

MessageBox :: Show(Fail);

}

else

MessageBox :: Show(请检查你的用户名和密码。);

} catch(异常^ ex){

MessageBox :: Show(ex-> Message);

}

}



我尝试了什么:



我尝试在线搜索,但我找不到解决方案

Below is the code i had written


private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
String^ constring=L"datasourse=localhost;port=3306;username=root;password=password";

MySqlConnection^ conDataBase=gcnew MySqlConnection(constring);<-(it display that the semicoloun with red line, the problem state that Argument Exception was unhandled, keyword not supported, Parameter name: datasource)

MySqlCommand^ cmdDataBase=gcnew MySqlCommand("select * from login_acc.login where Username='"+this->textBox1+"' and Password='"+this->textBox2->Text+"';",conDataBase);
MySqlDataReader^ myReader;
try{
conDataBase->Open();
myReader=cmdDataBase->ExecuteReader();
int count=0;
while(myReader->Read()){
count=count+1;

}
if(count==1){
MessageBox::Show("Login!");
}
else if(count>1){
MessageBox::Show("Fail");
}
else
MessageBox::Show("Pease check your usename and password.");
}catch(Exception^ex){
MessageBox::Show(ex->Message);
}
}

What I have tried:

I tried online searching but i can't find a solution

推荐答案

那是因为datasource拼写为c:

That's because "datasource" is spelled with a "c":
String^ constring=L"datasourse=localhost;port=3306;username=root;password=password";
                            ^
                            |



但是不要对连接字符串进行硬编码,这意味着每次发布代码时都必须更改代码。这意味着重新测试它,这次你正在测试生产数据库。危险!



还有其他一些东西:

1)永远不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。

2)控件的名称与Text属性不同:您的用户名将始终为System.Windows.Controls.Textbox......

3)切勿以明文形式存储密码 - 这是一个主要的安全风险。有关如何在此处执行此操作的信息:密码存储:如何做到这一点。 [ ^ ] - 它在C#中,但它仍然适用。


But don't hard-code connection strings, it means you have to change your code every time you release it. Which means re-testing it, and this time you are testing against the production database. Dangerous!

And a couple of other things:
1) Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
2) The name of a control is not the same as it's Text property: Your username will always be "System.Windows.Controls.Textbox"...
3) Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^] - it's in C# but it's all still applicable.


这篇关于当我尝试连接到mysql数据库时,以semicoloun获取错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆