标题:c#连接数据库出错
只看楼主
lukebc
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:3
帖 子:74
专家分:32
注 册:2009-8-18
结帖率:71.43%
 问题点数:0 回复次数:3 
c#连接数据库出错
using System;
using System.Collections.Generic;
using
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
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请输入名称");
                else
                {
                    try
                    {
                        string constr="server=;database="+textBox1.Text.Trim()+";uid=sa;pwd=";
                        SqlConnection conn=new SqlConnection(constr);
                        conn.Open();
                        if(conn.State==ConnectionState.Open)
                        {
                        label1.Text="["+textBox1.Text.Trim()+"]已经连接打开";
                        }
                }

                        catch
                    {
                           MessageBox.Show("数据库打开失败");
                        }
         }
        }
    }
    }
}

错误    1    只有 assignment、call、increment、decrement 和 new 对象表达式可用作语句   
错误    2    无效的表达式项“else"

错误    3 提示 else处要输入分号

我现在不知道怎么改,望哪位高手帮我
搜索更多相关主题的帖子: 连接数据库 private public 
2016-08-24 16:32
over1230
Rank: 8Rank: 8
等 级:贵宾
威 望:25
帖 子:113
专家分:897
注 册:2009-10-9
得分:0 
if (textBox1.Text == "")
            {
                MessageBox.Show("请输入名称");
            }
            else
            {
                try
                {
                    string constr = "server=;database=" + textBox1.Text.Trim() + ";uid=sa;pwd=";
                    SqlConnection conn = new SqlConnection(constr);
                    conn.Open();
                    if (conn.State == ConnectionState.Open)
                    {
                        label1.Text = "[" + textBox1.Text.Trim() + "]已经连接打开";
                    }
                }

                catch
                {
                    MessageBox.Show("数据库打开失败");
                }
            }
2016-08-24 17:04
redmarss
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2016-8-31
得分:0 
if没结束就套else?
2016-09-01 11:30
彷徨人生迷茫
Rank: 1
等 级:新手上路
帖 子:2
专家分:2
注 册:2016-9-1
得分:0 
if  else .........
2016-09-01 22:51



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-468158-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014087 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved