如题 我用的是visual studio自带的express,运行提示是数据库操作出错,不知为什么呀,请高手指点,谢谢!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.Data.SqlClient;namespace 病人管理系统
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
// int spara1 = textBox1.Text; //输入参数
// int? spara2 = 0; //输出参数 必须设为可空泛型并赋初值!!!
// this.QueriesTableAdapter string sex = "男";
if (radioButton2.Checked) sex = "女"; string name = "";
name = textBox3.Text; string patientNum = "";
patientNum = textBox1.Text; int age = 0;
if (textBox2.Text!=null)
age = Convert.ToInt16(textBox2.Text); if (age < 0 || age > 200)
{
MessageBox.Show("年龄范围错误!", "输入失败!",
MessageBoxButtons.OK, MessageBoxIcon.Information);
age = 0;
} string phenomenon = ""; string doctor = "";
doctor= comboBox1.SelectedItem.ToString();
string pclass = "轻微";
pclass= comboBox2.SelectedItem.ToString(); //创建connection对象
string connString = @"Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database1.mdf;Initial Catalog=病人;Integrate"+
"d Security=True;User Instance=True"; SqlConnection connection = new SqlConnection(connString); string sql = string.Format("INSERT INTO 病人(病人编号,姓名,年龄,性别,医生,类别,病症) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",
patientNum, name, age, sex, doctor, pclass, phenomenon); try
{
connection.Open();
//创建Command对象
SqlCommand command = new SqlCommand(sql, connection);
//执行添加命令,返回值为更新的行数
int count = command.ExecuteNonQuery();
if (count > 0)
{
MessageBox.Show("添加病人信息成功", "添加成功"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("添加病人信息失败", "添加失败"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "操作数据库出错!", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
finally
{
connection.Close();//关闭数据库连接
}
} private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
} private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
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.Data.SqlClient;namespace 病人管理系统
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
// int spara1 = textBox1.Text; //输入参数
// int? spara2 = 0; //输出参数 必须设为可空泛型并赋初值!!!
// this.QueriesTableAdapter string sex = "男";
if (radioButton2.Checked) sex = "女"; string name = "";
name = textBox3.Text; string patientNum = "";
patientNum = textBox1.Text; int age = 0;
if (textBox2.Text!=null)
age = Convert.ToInt16(textBox2.Text); if (age < 0 || age > 200)
{
MessageBox.Show("年龄范围错误!", "输入失败!",
MessageBoxButtons.OK, MessageBoxIcon.Information);
age = 0;
} string phenomenon = ""; string doctor = "";
doctor= comboBox1.SelectedItem.ToString();
string pclass = "轻微";
pclass= comboBox2.SelectedItem.ToString(); //创建connection对象
string connString = @"Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database1.mdf;Initial Catalog=病人;Integrate"+
"d Security=True;User Instance=True"; SqlConnection connection = new SqlConnection(connString); string sql = string.Format("INSERT INTO 病人(病人编号,姓名,年龄,性别,医生,类别,病症) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}')",
patientNum, name, age, sex, doctor, pclass, phenomenon); try
{
connection.Open();
//创建Command对象
SqlCommand command = new SqlCommand(sql, connection);
//执行添加命令,返回值为更新的行数
int count = command.ExecuteNonQuery();
if (count > 0)
{
MessageBox.Show("添加病人信息成功", "添加成功"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("添加病人信息失败", "添加失败"
, MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "操作数据库出错!", MessageBoxButtons.OK,
MessageBoxIcon.Exclamation);
}
finally
{
connection.Close();//关闭数据库连接
}
} private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
} private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
"d Security=True;User Instance=True";orstring connString =@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Initial Catalog=病人;Integrate"+
"d Security=True;User Instance=True";
如果数据库有密码输入密码