C#在编写winform的时候将数据插入sql数据库的时候的问题
插入的时候直接是insert后面的值,就是直接是valuse里面的值,而不是textbox里面的值源代码如下usingSystem;usingSystem.Collect...
插入的时候直接是insert 后面的值,就是直接是valuse里面的值,而不是textbox里面的值源代码如下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 Form3 : Form { public Form3() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { string leixing = txtleixing.Text; string usename = txtusename.Text; string passwowrd = txtpassword.Text; string shuoming = txtshuoming.Text; string str3 = "server=xiaobai;database=EnHeng;Integrated Security=SSPI;"; SqlConnection conn3 = new SqlConnection(str3); conn3.Open(); string strsql = "insert into lalala (账号类型,用户名,密码,说明) values ('@txtleixing.Text','@txtusename.Text','@txtpassword.Text','@txtshuoming.Text')"; SqlCommand cmd = new SqlCommand(strsql, conn3); cmd.ExecuteNonQuery(); conn3.Close(); } }}
展开
展开全部
string strsql = "insert into lalala (账号类型,用户名,密码,说明) values ('@txtleixing.Text','@txtusename.Text','@txtpassword.Text','@txtshuoming.Text')";
这一句改成
string strsql = string.Formate("insert into lalala (账号类型,用户名,密码,说明) values ('@{0}','@{1}','@{2}','@{3}')",txtleixing.Text,txtusename.Text,txtpassword.Text,txtshuoming.Text);
变量要用特殊符号转化才能拼到字符串中
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询