已有56人关注
C#16.1章节数据库连接问题
发表在C#图书答疑 2014-04-05
是否精华
版块置顶:
C#16.1章节问题

书上是这个代码,我要用windows认证登陆,请问怎么改呢?
服务器名是不是下图中的名称?还是ip地址


namespace ConSQL
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请输入要连接的数据库名称");
            }
            else
            {
                try
                {
                    string ConStr = "server=taoyu-pc;database=" + textBox1.Text.Trim() ;
                    SqlConnection conn = new SqlConnection(ConStr);
                    conn.Open();
                    if (conn.State == ConnectionState.Open)
                    {
                        label2.Text = "数据库【" + textBox1.Text.Trim() + "】已经连接并打开";
                    }
                }
                catch
                {
                    MessageBox.Show("连接数据库失败");
                }
            }
        }
    }
}

[img src=http://v2.freep.cn/3tb_140405113200ybds512293.jpg/img]
分享到:
精彩评论 1
小科_mrkj
学分:43 LV2
2014-04-09
沙发
读者朋友:
    您好,链接字符串改成Data Source=TAOYU-PC;Database=数据库名;Integrated Security=true;
首页上一页 1 下一页尾页 1 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照