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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?