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);
    }

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是整数类型,其余是字符串,如果不是,你引号要相应修改)

你这写法运行没报错也是奇怪:insert into YHGL(YHBH,YHMC,DLMM,YHJB,YHMS)values(UserID,UserMC,UserMM,UserJB,UserMS)

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="600" Width="800">

    <Button x:Name="btn1" Content="查询" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="157" Height="37" FontSize="20" Click="btn1_Click"/>
    <DataGrid x:Name="dataGrid" HorizontalAlignment="Left" Margin="10,56,0,0" VerticalAlignment="Top" Height="275" Width="772" SelectionChanged="dataGrid_SelectionChanged"/>
    <Button x:Name="btninsert" Content="添加" HorizontalAlignment="Left" Margin="10,359,0,0" VerticalAlignment="Top" Width="112" Height="50" FontSize="18" Click="btninsert_Click" RenderTransformOrigin="0.51,0.36"/>
    <TextBox x:Name="txtname" HorizontalAlignment="Left" Height="50" Margin="230,359,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="83" FontSize="18"/>
    <TextBox x:Name="txtage" HorizontalAlignment="Left" Height="50" Margin="472,359,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="83" FontSize="18"/>
    <TextBox x:Name="txtcou" HorizontalAlignment="Left" Height="50" Margin="699,359,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="83" FontSize="18"/>
    <Label Content="姓名" HorizontalAlignment="Left" Margin="142,359,0,0" VerticalAlignment="Top" Width="68" Height="50" FontSize="20" RenderTransformOrigin="0.5,0.5"/>
    <Label Content="年龄" HorizontalAlignment="Left" Margin="355,359,0,0" VerticalAlignment="Top" Width="76" Height="50" FontSize="20"/>
    <Label Content="选课" HorizontalAlignment="Left" Margin="577,359,0,0" VerticalAlignment="Top" Width="76" Height="50" FontSize="20"/>
    <Button x:Name="btndDelete" Content="删除" HorizontalAlignment="Left" Margin="201,10,0,0" VerticalAlignment="Top" Width="112" Height="37" FontSize="18" Click="btndDelete_Click" RenderTransformOrigin="0.51,0.36"/>
    <Button x:Name="btnupdate" Content="修改" HorizontalAlignment="Left" Margin="10,459,0,0" VerticalAlignment="Top" Width="112" Height="50" FontSize="18" Click="btnupdate_Click" RenderTransformOrigin="0.51,0.36"/>
    <Label Content="姓名" HorizontalAlignment="Left" Margin="142,459,0,0" VerticalAlignment="Top" Width="68" Height="50" FontSize="20" RenderTransformOrigin="0.5,0.5"/>
    <TextBox x:Name="txtnameupdate" HorizontalAlignment="Left" Height="50" Margin="230,459,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="83" FontSize="18"/>
    <Label Content="年龄" HorizontalAlignment="Left" Margin="355,459,0,0" VerticalAlignment="Top" Width="76" Height="50" FontSize="20"/>
    <TextBox x:Name="txtageupdate" HorizontalAlignment="Left" Height="50" Margin="472,459,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="83" FontSize="18"/>
    <Label Content="选课" HorizontalAlignment="Left" Margin="577,459,0,0" VerticalAlignment="Top" Width="76" Height="50" FontSize="20"/>
    <TextBox x:Name="txtcouupdate" HorizontalAlignment="Left" Height="50" Margin="699,459,0,0" TextWrapping="Wrap"  VerticalAlignment="Top" Width="83" FontSize="18"/>

</Grid>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

using System.Data.SqlClient;
using System.Data;namespace shujuku
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

    private void btn1_Click(object sender, RoutedEventArgs e)
    {
        dataGrid.ItemsSource = GetStudet();
    }
    private List<Student> GetStudet()
    {  List<Student> stdents=new List<Student>();
        string mySQ = "Data Source=127.0.0.1; Database=text23; User ID=sa; Password=123456";
        SqlConnection conn = new SqlConnection(mySQ);
        string sql=@"select * from student";
        conn.Open();
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataReader reader    = cmd.ExecuteReader();
        while(reader.Read())
        {
            Student stu = new Student();
            stu.ID=Convert.ToInt32(reader["id"]);
            stu.Age = reader["age"].Equals(DBNull.Value)?0:Convert.ToInt32(reader["age"]);
            stu.Name = reader["name"].ToString();
            stu.Cum = reader["cou234rid"].Equals(DBNull.Value) ? 0 : Convert.ToInt32(reader["cou234rid"]);
            stdents.Add(stu);
        } 
          reader.Close();
          conn.Close();
          return stdents;

    }

    private void btninsert_Click(object sender, RoutedEventArgs e)
    {
        string name = txtname.Text;
        int age = Convert.ToInt32(txtage.Text);
        int cou = Convert.ToInt32(txtcou.Text);
        string sql = @"insert into student(name ,age ,cou234rid) values('" + name + "'," + age + "," + cou + ")";
        if (ExecNoQuery(sql))
        {
            dataGrid.ItemsSource = GetStudet();
        }
    }

    private bool ExecNoQuery(string sql)
    {
        int result=0;
        try
        {
            string mySQ = "Data Source=127.0.0.1; Database=text23; User ID=sa; Password=123456";
            SqlConnection conn = new SqlConnection(mySQ);
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            result = cmd.ExecuteNonQuery();
            conn.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        return result > 0 ? true : false;
    }

    private void btndDelete_Click(object sender, RoutedEventArgs e)
    {
        if (dataGrid.SelectedItem != null)
        {
            Student stu = (Student)dataGrid.SelectedItem;
            string sql = @"delete from student where id=" + stu.ID;
            if (ExecNoQuery(sql))
            {
                dataGrid.ItemsSource = GetStudet();
            }
        }

    }

    private void btnupdate_Click(object sender, RoutedEventArgs e)
    {

        if (dataGrid.SelectedItem != null)
        {
            Student stu = (Student)dataGrid.SelectedItem;

            string name = txtnameupdate.Text;
            int age = Convert.ToInt32(txtageupdate.Text);
            int cou = Convert.ToInt32(txtcouupdate.Text);
            string sql = @"update student set  name ='" + name + " ',age=" + age + ",cou234rid=" + cou + " where id ="+stu.ID;
            if (ExecNoQuery(sql))
            {
                dataGrid.ItemsSource = GetStudet();
            }
        }
        txtnameupdate.Text = "";
        txtageupdate.Text = "";
        txtcouupdate.Text = "";
    }

    private void dataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (dataGrid.SelectedItem != null)
        {
            Student stu = (Student)dataGrid.SelectedItem;
             txtnameupdate.Text =stu.Name ;
             txtageupdate.Text =Convert.ToString( stu.Age);
             txtcouupdate.Text = Convert.ToString(stu.Cum);
        }
    }



}

}
这个是我之前刚学的文件,你可以参考下!