各位高手帮忙看看如何用C#向SQL数据库添加数据
不好用,点击添加后没有反应,希望帮忙using System;--------------------编程问答--------------------
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsApplication2
{
public partial class 加工企业 : Form
{
//定义连接SQL Server数据库字符串
string consqlserver = "Data Source=DADI-PC\\SQLEXPRESS;Initial Catalog=MYDATABASE;Integrated Security=True";
public 加工企业()
{
InitializeComponent();
}
private void cancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void 加工企业_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
//定义连接数据源
string sql = "select * from 加工企业数据";
//定义SQL Server连接对象
SqlConnection con = new SqlConnection(consqlserver);
SqlDataAdapter da = new SqlDataAdapter(sql, con);
try
{
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
//将获得数据源赋予数据库控件
dataGridView1.DataSource = ds.Tables[0];
}
}
catch (Exception ex)
{
throw new Exception(ex.ToString());
}
finally
{
con.Close();
con.Dispose();
da.Dispose();
}
}
private void tj_Click(object sender, EventArgs e)
{
try
{
// 首先判断输入信息是否完全
if (tb原料编号.Text == "" || tb记录时间.Text == "" || tb记录人员.Text == "" || tb加工时间.Text == "" || tb车间编号.Text == "" || tb加工人员.Text == "" || tb操作时间.Text == "" || tb车间温度.Text == "" || tb包装规格.Text == "" || tb企业名称.Text == "" || tb加工工艺.Text == "")
{
MessageBox.Show("请输入完整的加工信息", "信息提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
}
else
{
SqlConnection conn = new SqlConnection ( "Data Source=DADI-PC\\SQLEXPRESS;Initial Catalog=MYDATABASE;Integrated Security=True");
conn.Open();
string sqlStr = "insert into 加工企业数据(原料编号,企业名称,记录时间,记录人员,加工时间,车间编号,加工工艺,加工人员,操作时间,车间温度,包装规格) values (tb原料编号.Text,tb企业名称.Text,tb记录时间.Text,tb记录人员.Text,tb加工时间.Text,tb车间编号.Text,tb加工工艺.Text,tb加工人员.Text,tb操作时间.Text,tb车间温度.Text,tb包装规格.Text)";
SqlCommand cmd = new SqlCommand(sqlStr, conn);
cmd.ExecuteScalar();
conn.Close();
MessageBox.Show("添加成功", "信息提示", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Warning);
}
}
catch { }
}
}
}
不要try掉 或者你把异常抛出来啊 才知道哪里出问题了么...
你这代码 怎么那么多中文 连字段都是中文的 接受不了 --------------------编程问答-------------------- 研究一下cmd.ExecuteScalar();是实现什么功能的,好吧 --------------------编程问答-------------------- ExecuteNonQuery();//
MSDN,好好研究一下SqlCommand吧 --------------------编程问答--------------------
string sqlStr = "insert into 加工企业数据(原料编号,企业名称,记录时间,记录人员,//这个SQL语句有问题,变量不是这样的,要用途‘“+tb记录人员.Text,tb加工时间.Text+”’这样的格式! --------------------编程问答-------------------- mystring就是 用sql语言 insert into...
加工时间,车间编号,加工工艺,加工人员,操作时间,车间温度,包装规格) values (tb原料编号.Text,tb企业名
称.Text,tb记录时间.Text,tb记录人员.Text,tb加工时间.Text,tb车间编号.Text,tb加工工艺.Text,tb加
工人员.Text,tb操作时间.Text,tb车间温度.Text,tb包装规格.Text)";
constr就用连接字符串
void sqlexcuted(string mystring)--------------------编程问答-------------------- 同意4楼的,你的SQL语句中values后面的参数值赋值方法错了 --------------------编程问答--------------------
{
SqlConnection con = new SqlConnection(constr);
string sql = mystring ;
SqlCommand com = new SqlCommand(sql, con);
try
{
con.Open();
com.ExecuteScalar();
// MessageBox.Show("11");
}
catch (Exception)
{
throw;
}
finally
{
con.Close();
}
}
本人菜鸟一个,去掉try了,没有报错,还是不明白,还希望大神指教,谢谢了 --------------------编程问答--------------------
已经改了,但还是不好用呀,本人菜鸟,希望多多指教,谢谢了 --------------------编程问答--------------------
没看懂呀,本人菜鸟,希望多多指教,谢谢了 --------------------编程问答-------------------- [code=csharp][执行写入.CommandText = "INSERT INTO [sheet1$] (省, 市, 区、县, 镇、乡, 街、路、村, ☆, 自然村, 村民组, 门牌号, 原字符☆替字符, 序) VALUES ('"
35. + 数据表.Rows[行][0] + "','" + 数据表.Rows[行][1] + "','" + 数据表.Rows[行][2] + "','"
36. + 数据表.Rows[行][3] + "','" + 数据表.Rows[行][4] + "','" + 数据表.Rows[行][5] + "','"
37. + 数据表.Rows[行][6] + "','" + 数据表.Rows[行][7] + "','" + 数据表.Rows[行][8] + "','"
38. + 数据表.Rows[行][9] + "','序" + (行 + 1).ToString() + "')";
/code] --------------------编程问答-------------------- 语法通用参考
补充:.NET技术 , C#