...
using System.Data;
using System.Data.OleDb;
namespace accessloginapp
{
public partial class Ramen : Form
{
private OleDbConnection connection = new OleDbConnection();
public Ramen()
{
InitializeComponent();
connection.ConnectionString =
@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\...\Users.accdb;Persist Security Info=False;";
}
private void btn_Save_Click(object sender, EventArgs e)
{
try{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
command.CommandText =
"insert into userdata (Username,[Password]) values('" +
txt_Username + "','" + txt_Password + "')";
command.ExecuteNonQuery();
MessageBox.Show("Users added and saved");
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show("Error" + ex);
}
}
}
}
很抱歉,如果我不太了解,我对此很陌生。当我在应用程序中保存诸如用户名和密码之类的数据时,数据将按我输入的内容插入,但带有添加的文本,例如:我将发送"Mark"
要插入的用户名,但是当我查看数据库时,将其放入在作为"System.Windows.Forms.TextBox, Text: Mark"
。如何将其更改为仅插入我输入的用户名?
您需要使用Text
文本框控件的属性来获取存储的实际文本:-
command.CommandText = "insert into userdata (Username,[Password])
values('" + txt_Username.Text + "','" + txt_Password.Text + "')";
除此之外,请注意,您的查询已针对SQL Injection攻击开放。
因此,您应该使用参数化查询,例如:
command.CommandText = "insert into userdata (Username,[Password])
values(?,?)";
command.Parameters.Add("?",OleDbType.VarChar,20).Value = txt_Username.Text;
并类似地为添加参数@Password
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句