作为练习 ,写了一个往数据库里添加信息的小程序,可惜数据库无法链接上,才学不久,还请大侠指教。我邮箱[email protected] ,帮忙改一下,指出错误,然后发我,我好好学习下。文件地址:
 Hi,推荐文件给你 "WindowsFormsApplication3.rar" http://vdisk.weibo.com/s/go3Fj

解决方案 »

  1.   

    connString里面少写东西了吧。看看服务器名对不对,用户名密码都写了没
      

  2.   

    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 WindowsFormsApplication3
    {
        public partial class Form1 : Form
        {
            DataSet ds;
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                if (textBox1.Text.Trim() == "" || textBox5.Text == "" || textBox4.Text == "" || textBox3.Text == "" || textBox2.Text == "" || textBox6.Text == "" || textBox7.Text == "" || textBox8.Text == "" || textBox9.Text == "" || textBox10.Text == "" || textBox11.Text == "" || textBox12.Text == "" || textBox13.Text == "" || textBox14.Text == "" || textBox15.Text == "" || textBox16.Text == "")
                {
                    MessageBox.Show("请输入完整信息", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    textBox1.Focus();
                }
                else
                {
                    string connString = "server=.;database=医院系统数据库;integrated security=true";
                    SqlConnection myConn6 = new SqlConnection(connString);
                    myConn6.Open();
                    string sql1 = "select * from users where user_id='" + textBox16.Text.Trim() + "'";
                    SqlCommand scmd = new SqlCommand(sql1, myConn6);
                    string sql2 = "select * from users where user_id='" + textBox10.Text.Trim() + "'";
                    SqlCommand scme = new SqlCommand(sql2, myConn6);
                    string sql3 = "select * from users where user_id='" + textBox10.Text.Trim() + "'";
                    SqlCommand scmf = new SqlCommand(sql3, myConn6);
                    if (null == scmd.ExecuteScalar() & null == scme.ExecuteScalar())
                    {
                        sql1 = "insert into users values('" + textBox16.Text.Trim() + "','" + textBox1.Text.Trim() + "','" + textBox2.Text.Trim() + "','" + textBox3.Text.Trim() + "','" + textBox4.Text.Trim() + "','" + textBox5.Text.Trim() + "')";
                            scmd.CommandText = sql1;
                            scmd.ExecuteNonQuery();
                            sql2 = "insert into familys values('" + textBox10.Text.Trim() + "','" + textBox9.Text.Trim() + "','" + textBox8.Text.Trim() + "','" + textBox7.Text.Trim() + "','" + textBox6.Text.Trim() + "')";
                            scme.CommandText = sql2;
                            scme.ExecuteNonQuery();
                            sql3 = "insert into case values('" + textBox11.Text.Trim() + "','" + textBox12.Text.Trim() + "','" + textBox13.Text.Trim() + "','" + textBox14.Text.Trim() + "','" + textBox15.Text.Trim() + "')";
                            scmf.CommandText = sql3;
                            scmf.ExecuteNonQuery();
                            MessageBox.Show("添加成功!", "提示");
                        
                    }
                }
            }
                    
                    private void button2_Click(object sender, EventArgs e)
            {
                this.Close();
            }        private void label1_Click(object sender, EventArgs e)
            {        }        private void label2_Click(object sender, EventArgs e)
            {        }        private void Form1_Load(object sender, EventArgs e)
            {        }        private void label6_Click(object sender, EventArgs e)
            {        }        private void label16_Click(object sender, EventArgs e)
            {        }        private void textBox1_TextChanged(object sender, EventArgs e)
            {        }        private void textBox15_TextChanged(object sender, EventArgs e)
            {        }        private void textBox3_TextChanged(object sender, EventArgs e)
            {        }
        }
    }
      

  3.   

    你打开连接太快了吗?对吗?我不是很清楚,你试试? 把它myConn6.Open(); 放在查询命令(SqlCommand)之后。