如何解决连接字符串的异常(本地数据库c#)

时间:2018-03-09 10:53:57

标签: c# mysql database

好吧所以第一个问题是连接字符串本身它有这个我不明白的异常所以我试图把它放在try catch语法中,但是当我将它插入公共部分类Form1时:形式括号是代理我把它插入一个函数中,现在函数有这个错误:

严重级代码描述项目文件行抑制状态 错误CS0161'Form1.connection()':并非所有代码路径都返回值餐厅管理系统C:\ Users \ admin \ source \ repos \ Restaurant Management System \ Restaurant Management System \ Form1.cs 36 Active

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Restaurant_Management_System
{
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
            panel1.BackColor = Color.FromArgb(50, Color.Black);
            label1.BackColor = Color.FromArgb(30, Color.Beige);
            label2.BackColor = Color.FromArgb(0, Color.Black);
            Password.BackColor = Color.FromArgb(0, Color.Black);

        }


        SqlCommand cmd;
        SqlDataReader dr;



        public SqlConnection connection()
        {
            try
            {
                 SqlConnection con = new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename= \"|Data Directory|\\Coffee(LoginEmployee).mdf\";Integrated Security=True;");

            }
            catch (Exception ex)
            {
                MessageBox.Show("Error message: COULD NOT CONNECT STRING: " + ex);
            }
        }


        private string getUsername()
        {
           SqlConnection con = connection();

            cmd = new SqlCommand("SELECT nalue FROM EmployeeLog where Property=Username", con);
            dr = cmd.ExecuteReader();
            dr.Read();
            return dr[0].ToString();
        }

        private string getPassword()
        {
            SqlConnection con = connection();
            cmd = new SqlCommand("SELECT nalue FROM EmployeeLog where Property=Password", con);
            dr = cmd.ExecuteReader();
            dr.Read();
            return dr[0].ToString();
        }

我需要更换什么?为什么不是全部都返回一个值?如果我使用void case它也会有这个错误,我无法显式转换为sqlconnection。这是在最新的视觉工作室2017年制作的

1 个答案:

答案 0 :(得分:0)

如果捕获异常,则不会返回SqlConnection。因此,您可以在显示消息框后返回null。

当然,在调用connection()之后,您需要进行空检查,这样就不会尝试使用它来获取空引用异常。

您还需要返回正在创建的连接:

return new SqlConnection("Data Source=(LocalDB)\\MSSQLLocalDB;AttachDbFilename=|Data Directory|Coffee(LoginEmployee).mdf;Integrated Security=True;");

注意:我不建议对连接字符串进行硬编码!您通常会将连接字符串添加到app.config / web.config并使用ConfigurationManager.ConnectionStrings读取它... - 这是因为您可能在不同的计算机上有不同的实例名称,或者您可能希望指向数据库服务器而不是本地服务器。您无需更改代码并重新编译,只需使其在多台计算机上运行即可。

有关microsoft类库网站(https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnectionstringbuilder.attachdbfilename(v=vs.110).aspx)的信息说:An error will be generated if a log file exists in the same directory as the data file and the 'database' keyword is used when attaching the primary data file. In this case, remove the log file. Once the database is attached, a new log file will be automatically generated based on the physical path.