我有下面的代碼添加一個新用戶:在形式錯誤在連接簡單引號數據庫
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
public class users
{
public Sqlconnection myconn()
{
return new ("data source=.; integrated security=true; initial catalog=test;");
}
public bool insertuser(username, pass, type)
{
try {
string query="insert into users (username, pass, type) values ('"+username+"', '"+pass+"', '"+type+"');
return true;
SqlCommand mycommand = new SqlCommand (query, this.myconn);
mycommand.Connection.Open();
mycommand.ExecuteNonQuery();
mycommand.Connection.Close();
return true;
}
catch {
return false;
}
}
}
現在如果用戶調用此方法
users user1 = new users();
if(user1.insertuser(txtusername.tex, txtpass.text, cbtype.text)==true)
{
// BUG IS HERE IF USER WRITE SOMETHING SO.. ANGEL' (WITH THIS ')
// MY CODE IS GOING TO HAVE A BUG!
// I QUIT THEM IN KEY PRESS BUT WHAT HAPPEND IF USERS MUST TO ADD SOMETHING AS
// tic's
// what can i do for my code acept all?? and it doesn't have any bug?
MessageBox.show("user added");
}
@angel - 它爲你工作? – 2011-05-23 16:33:28