C#中想做把textbox中的信息点击按钮添加到数据库中,但是一直出错,大神可以帮忙看看嘛
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Da...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textbox1.Text == "" || textbox2.Text == "" || textbox3.Text == "" || textbox4.Text == "" || textbox5.Text == "")
MessageBox.Show("不能有空值!");
else
{
String ename = textbox1.Text;
String epassword = textbox2.Text;
String eidcard = textbox3.Text;
String ephonenumber = textbox4.Text;
String eadress = textbox5.Text;
string con = "Data Source=localhost;Initial Catalog=bmxt;Integrated Security=True";
SqlConnection conn = new SqlConnection(con);
conn.Open();
SqlCommand command = conn.CreateCommand();
String Sql = "insert into student (name,password,idcard,phonenumber,adress )values(" + textbox1.Text + ",'" + textbox2.Text + "','" + textbox3 + "','" + textbox4.Text + "','" + textbox5.Text + "')";
//String Sql = "insert into information values(" + ename + ",'" + epassword + "','" + eidcard + "','" + ephonenumber + "','" + eadress + "','');";
command.CommandText = Sql;
int Row = 0;
try
{
Row = command.ExecuteNonQuery();//返回行数
//取出返回值
if (Row > 0)
{
MessageBox.Show("注册成功!");
}
}
catch (Exception er)
{
string merr = "err:" + er.ToString();
MessageBox.Show(merr);
}
finally
{
conn.Close();
conn.Dispose();//释放所有资源
command.Parameters.Clear();
command.Dispose();//释放所有资源
}
}
}
}
} 展开
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textbox1.Text == "" || textbox2.Text == "" || textbox3.Text == "" || textbox4.Text == "" || textbox5.Text == "")
MessageBox.Show("不能有空值!");
else
{
String ename = textbox1.Text;
String epassword = textbox2.Text;
String eidcard = textbox3.Text;
String ephonenumber = textbox4.Text;
String eadress = textbox5.Text;
string con = "Data Source=localhost;Initial Catalog=bmxt;Integrated Security=True";
SqlConnection conn = new SqlConnection(con);
conn.Open();
SqlCommand command = conn.CreateCommand();
String Sql = "insert into student (name,password,idcard,phonenumber,adress )values(" + textbox1.Text + ",'" + textbox2.Text + "','" + textbox3 + "','" + textbox4.Text + "','" + textbox5.Text + "')";
//String Sql = "insert into information values(" + ename + ",'" + epassword + "','" + eidcard + "','" + ephonenumber + "','" + eadress + "','');";
command.CommandText = Sql;
int Row = 0;
try
{
Row = command.ExecuteNonQuery();//返回行数
//取出返回值
if (Row > 0)
{
MessageBox.Show("注册成功!");
}
}
catch (Exception er)
{
string merr = "err:" + er.ToString();
MessageBox.Show(merr);
}
finally
{
conn.Close();
conn.Dispose();//释放所有资源
command.Parameters.Clear();
command.Dispose();//释放所有资源
}
}
}
}
} 展开
1个回答
展开全部
你先说问题出在哪里,或者你调试的时候出现什么问题。我再分析代码。
更多追问追答
追答
String Sql = "insert into student (name,password,idcard,phonenumber,adress )values(" + textbox1.Text + ",'" + textbox2.Text + "','" + textbox3 + "','" + textbox4.Text + "','" + textbox5.Text + "')";
你就不能调试的时候看一下构造的sql语句正确不吗?你看你的这一句 看到textbox1.Text,后面加了单引号,前面呢??字符串格式的必须要带单引号才能往里面入,你既然是张三,就应该是values('" +"'" textbox1.Text + "','"+......这样你再试试,还有你的textbox3后面不加.Text?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询