static public SqlConnection GetConn()
{
try
{
string sql = null;
SqlConnForm form = new SqlConnForm();
if (form.radioButtonWin.Checked == true && SqlConnForm.dbname == "")
{
sql = @"Data Source=" + SqlConnForm.sername + ";Initial Catalog=master;Integrated Security=True";
SqlConnection conn = new SqlConnection(sql);
return conn;
}
if (form.radioButtonWin.Checked == true && SqlConnForm.dbname != "")
{
sql = @"Data Source=" + SqlConnForm.sername + ";Initial Catalog=" + form.comboBoxDbName.Text + ";Integrated Security=True";
SqlConnection conn = new SqlConnection(sql);
return conn;
}
if (form.radioButtonSQL.Checked == true && SqlConnForm.dbname == "")
{
sql = @"Data Source=" + SqlConnForm.sername + ";Initial Catalog=master;User ID=" + SqlConnForm.username + "Pwd=" + SqlConnForm.password + ";";
SqlConnection conn = new SqlConnection(sql);
return conn;
}
if (form.radioButtonSQL.Checked == true && SqlConnForm.dbname != "")
{
sql = @"Data Source=" + SqlConnForm.sername + ";Initial Catalog=" + SqlConnForm.dbname + ";User ID=" + SqlConnForm.username + "Pwd=" + SqlConnForm.password + ";";
SqlConnection conn = new SqlConnection(sql);
return conn;
}
}
catch (Exception e)
{
throw e;
}
c# 提示并非所有的代码路径都返回值,这段代码哪不对
答案:2 悬赏:60 手机版
解决时间 2021-02-15 13:26
- 提问者网友:蓝琪梦莎
- 2021-02-15 02:42
最佳答案
- 五星知识达人网友:污到你湿
- 2021-02-15 03:21
你把return conn放到外面不就行了,还有你最好把sqlconnection弄成全局的
尝试修改成下面的代码,试一下
static public SqlConnection GetConn()
{
try
{
SqlConnection conn=null;
string sql = null;
SqlConnForm form = new SqlConnForm();
if (form.radioButtonWin.Checked == true && SqlConnForm.dbname == "")
{
sql = @"Data Source=" + SqlConnForm.sername + ";Initial Catalog=master;Integrated Security=True";
conn = new SqlConnection(sql);
}
if (form.radioButtonWin.Checked == true && SqlConnForm.dbname != "")
{
sql = @"Data Source=" + SqlConnForm.sername + ";Initial Catalog=" + form.comboBoxDbName.Text + ";Integrated Security=True";
conn = new SqlConnection(sql);
}
if (form.radioButtonSQL.Checked == true && SqlConnForm.dbname == "")
{
sql = @"Data Source=" + SqlConnForm.sername + ";Initial Catalog=master;User ID=" + SqlConnForm.username + "Pwd=" + SqlConnForm.password + ";";
conn = new SqlConnection(sql);
}
if (form.radioButtonSQL.Checked == true && SqlConnForm.dbname != "")
{
sql = @"Data Source=" + SqlConnForm.sername + ";Initial Catalog=" + SqlConnForm.dbname + ";User ID=" + SqlConnForm.username + "Pwd=" + SqlConnForm.password + ";";
conn = new SqlConnection(sql);
}
}
catch (Exception e)
{
throw e;
}
return conn;
尝试修改成下面的代码,试一下
static public SqlConnection GetConn()
{
try
{
SqlConnection conn=null;
string sql = null;
SqlConnForm form = new SqlConnForm();
if (form.radioButtonWin.Checked == true && SqlConnForm.dbname == "")
{
sql = @"Data Source=" + SqlConnForm.sername + ";Initial Catalog=master;Integrated Security=True";
conn = new SqlConnection(sql);
}
if (form.radioButtonWin.Checked == true && SqlConnForm.dbname != "")
{
sql = @"Data Source=" + SqlConnForm.sername + ";Initial Catalog=" + form.comboBoxDbName.Text + ";Integrated Security=True";
conn = new SqlConnection(sql);
}
if (form.radioButtonSQL.Checked == true && SqlConnForm.dbname == "")
{
sql = @"Data Source=" + SqlConnForm.sername + ";Initial Catalog=master;User ID=" + SqlConnForm.username + "Pwd=" + SqlConnForm.password + ";";
conn = new SqlConnection(sql);
}
if (form.radioButtonSQL.Checked == true && SqlConnForm.dbname != "")
{
sql = @"Data Source=" + SqlConnForm.sername + ";Initial Catalog=" + SqlConnForm.dbname + ";User ID=" + SqlConnForm.username + "Pwd=" + SqlConnForm.password + ";";
conn = new SqlConnection(sql);
}
}
catch (Exception e)
{
throw e;
}
return conn;
全部回答
- 1楼网友:人间朝暮
- 2021-02-15 04:37
这么和你说吧,你定义 了一个有返回值的函数,那么就一定要有返回值,现在是你只在try里返回了,如果当然try不成功(就是有错,有异常),那么就要中间断开,goto到catch,这样try里就没有返回,现在你catch里又没有返回,到函数最后也没有,那么这个函数到最后都没有返回值,如果编译器认你通过的话,如果try 出现异常,那么这个函数就不会有返值,那就是说函数没有停止,也就是说,如何在这一线程下的就这样停止,如果是主线程的话那就是程序卡在这里了,没反响, 以上都是一些啰嗦话,明白就好,像try catch 和if else 等一些判断语句,多个的还是一个的,只要函数返回值不是void 那么 每个判断都要有return .当然了如果在函数最后return 以上的都是废话
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯