LIANGJUN369
L_JUN1758
采纳率100%
2019-02-21 11:19 浏览 619

C# 学生成绩管理系统 向数据库中添加数据失败

10

刚学C#的小白一个 有好多问题都不太懂 希望各位大神忍受一下哈 虚心求教
C#学生成绩管理系统 连接数据库 向数据库写入学生信息失败

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 WindowsApplication1
{
public partial class XinXiShuRu : Form
{

    public XinXiShuRu()
    {
        InitializeComponent();   

    }

    private void XinXiShuRu_Load(object sender, EventArgs e)
    {


    }

    private void button1_Click(object sender, EventArgs e)
    {
        try
        {
            string strcon = "Server=.;database=StudentCj;Integrated Security=true";
            SqlConnection con;
            con = new SqlConnection(strcon);
            DataSet sd = new DataSet();
            string sqlbj = "select bjdm from bj where bjmc='" + comboBox1.Text + "'";
            string id = "select* student from id='" + textBox1 + "'";
            SqlDataAdapter sda = new SqlDataAdapter(id, con);
            sd.Clear();
            sda.Fill(sd, "id");

            if (sd.Tables[0].Rows.Count != 0)
            {

                MessageBox.Show("你输入的信息有误");
                                }
            else
            {
                SqlDataAdapter sda1 = new SqlDataAdapter(sqlbj, con);
                sd.Clear();
                sda1.Fill(sd, "bjmc");
                string bjcx = sd.Tables[0].Rows[0][0].ToString();
                string sql = "insert into student(id,name,bjdm) values('" + textBox1.Text + "','" + textBox2.Text + "','" + bjcx + "')";
                SqlCommand com = new SqlCommand(sql, con);
                con.Open();
                com.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("恭喜你!输入成功!");
                textBox1.Text = "";
                textBox2.Text = ""; 
            }

        }
        catch
        {
            MessageBox.Show("你输入的信息有误");
        }

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    private void textBox2_TextChanged(object sender, EventArgs e)
    {

    }

}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答 默认 最新

  • 已采纳
    caozhy 从今以后生命中的每一秒都属于我爱的人 2019-02-21 11:22
            string id = "select* student from id='" + textBox1 + "'";
            SqlDataAdapter sda = new SqlDataAdapter(id, con);
            sd.Clear();
            sda.Fill(sd, "id");
    

    既然你的表名叫做student,为什么你下面Fill的时候变成id了。

    点赞 评论 复制链接分享
  • hxycsdn9159 hxycsdn9159 2019-02-21 15:55

    string id = "select* student from id='" + textBox1 + "'"; 这一句应该是 string id = "select* student from id='" + textBox1.Text + "'";吧?
    你怎么能直接把textBox1这个控件拼到字符串里面,应该拼接的是textBox1.Text吧?

    点赞 评论 复制链接分享
  • qq_35812766 杰geg 2019-02-22 13:28

    你的代码还有问题吗?有的话可以粘贴出来看一下

    点赞 评论 复制链接分享

相关推荐