C#向数据库存数据为什么我的程序执行后数据库数据没有变?
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 WindowsFormsApplication11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection conn;
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=abc;User ID=wang;password=19931206wyxgzhg");
conn.Open();
string num = textBox1.Text.Trim();
try
{
string inser = "insert into shuju(data) values (@num)";
if (textBox1.Text != "")
{
using (SqlCommand cmd = new SqlCommand(inser, conn))
{
SqlParameter p1 = new SqlParameter("@num", num);
cmd.Parameters.Add(p1);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
} 展开
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 WindowsFormsApplication11
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection conn;
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=abc;User ID=wang;password=19931206wyxgzhg");
conn.Open();
string num = textBox1.Text.Trim();
try
{
string inser = "insert into shuju(data) values (@num)";
if (textBox1.Text != "")
{
using (SqlCommand cmd = new SqlCommand(inser, conn))
{
SqlParameter p1 = new SqlParameter("@num", num);
cmd.Parameters.Add(p1);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
} 展开
1个回答
2015-12-11
展开全部
你没执行命令啊,
using (SqlCommand cmd = new SqlCommand(inser, conn))
{
SqlParameter p1 = new SqlParameter("@num", num);
cmd.Parameters.Add(p1);
cmd.ExecuteNonQuery(); //加上这句。
}
using (SqlCommand cmd = new SqlCommand(inser, conn))
{
SqlParameter p1 = new SqlParameter("@num", num);
cmd.Parameters.Add(p1);
cmd.ExecuteNonQuery(); //加上这句。
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询