注册 登录
编程论坛 SQL Server论坛

为什么连接不到数据库

编程3869768 发布于 2008-11-22 14:07, 2329 次点击
我做啦一个数据库 Text   在windows 应用程序下  
通过用户界面  进行添加, 删除  不知道哪里出啦问题
总是连接不到数据库 郁闷   
只有本站会员才能查看附件,请 登录

帮我看看  
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private SqlConnection myConnection;
        private SqlCommand myCommand;

        private void Form1_Load(object sender, EventArgs e)
        {
            myConnection = new SqlConnection ("data source=(.);Initial Catalog=Text;Integrated Security=SSPI ");
               
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                myConnection.Open();
                string sql = "insert into Passenger_table values('" + this.textBox1.Text + " ','" + this.textBox2.Text + " ','" + this.textBox3.Text + " ','" + this.textBox4.Text + "','" + this.textBox5.Text + "')";
                myCommand = new SqlCommand(sql, myConnection);
                myCommand.ExecuteNonQuery();
                MessageBox.Show("插入成功");
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                myConnection.Close();
            }
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox4_TextChanged(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            




        }

        private void textBox5_TextChanged(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                myConnection.Open();
                myCommand = new SqlCommand("delect * from Passenger_table where flightID='" + textBox1 + "'", myConnection);
                myCommand.ExecuteNonQuery();
                MessageBox.Show("删除成功");
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                myConnection.Close();
            }
           
 
        
        }
    }
}
7 回复
#2
小仙2008-11-22 14:13
data source=(.);改成server = .;试下,晕
ADO的帖子发到这来了。
#3
编程38697682008-11-22 14:54

LZ  
太感谢啦    成功啦
对啦 可以告诉我为什么 这样吗  
我写的 在语法上没有问题啦  
data source=(.)   和 server=.    为什么  改变这么一点  
就有这么大的区别
请解释一下
#4
小仙2008-11-22 22:01
你就这样记好了。。
我也不清楚它为什么要这样。。
#5
dxformat2008-11-27 08:54
呵呵,server=. 这是标准格式,我记得书上就是这样教的,没啥为什么,就是这种写法
#6
mc204212192008-12-09 15:22
顶!
#7
球球2008-12-10 00:12
哈哈。。。~~
1