刚学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)
{
}
}
}