A_syy 2019-05-05 20:26 采纳率: 0%
浏览 945

C#中向数据库添加数据,SQL数据库对应表格没有添加相应记录怎么解决?

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.Collections;
using System.Data.SqlClient;

namespace GHDBMS
{
public partial class 用户权限管理 : Form
{
public 用户权限管理()
{
InitializeComponent();
}

    private void 用户权限管理_Load(object sender, EventArgs e)
    {
        string strConnection = "Data Source=SYY\\SQLEXPRESS;Initial Catalog=DB;Integrated Security=True";
        SqlConnection objConnection = new SqlConnection(strConnection);
        objConnection.Open();
        SqlDataAdapter sda = new SqlDataAdapter("select * from YHGL",objConnection);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        用户列表.DataSource = dt;
        修改.Enabled = false;
        删除.Enabled = false;
        objConnection.Close();
    }

    private void 添加_Click(object sender, EventArgs e)
    {
        string UserID = textBox1.Text;
        string UserMC = textBox2.Text;
        string UserMM = textBox3.Text;
        string UserJB = comboBox1.Text;
        string UserMS = richTextBox1.Text;
        string strConnection = "Data Source=SYY\\SQLEXPRESS;Initial Catalog=DB;Integrated Security=True";
        SqlConnection objConnection = new SqlConnection(strConnection);
        objConnection.Open();
        SqlCommand com = new SqlCommand("insert into YHGL(YHBH,YHMC,DLMM,YHJB,YHMS)values(UserID,UserMC,UserMM,UserJB,UserMS)", objConnection);
        com.ExecuteNonQuery();
        objConnection.Close();
        用户权限管理_Load(sender,e);
    }

  • 写回答

3条回答 默认 最新

  • threenewbee 2019-05-05 20:40
    关注

    new SqlCommand("insert into YHGL(YHBH,YHMC,DLMM,YHJB,YHMS)values(UserID,UserMC,UserMM,UserJB,UserMS)"
    ->
    new SqlCommand("insert into YHGL(YHBH,YHMC,DLMM,YHJB,YHMS)values(" + UserID + ",'" + UserMC + "','" + UserMM + "','" + UserJB + "','" + UserMS + "')"

    (假设UserID是整数类型,其余是字符串,如果不是,你引号要相应修改)

    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题