连接字符串不正确,如何解决?

时间:2017-07-01 13:58:48

标签: c# .net visual-studio

我正在使用Visual Studio 2017社区,并且我正在尝试创建本地数据库,而我在连接字符串中遇到问题,因为它在运行时无法连接将其卡在conn.Open();

这是连接的代码和executiontenonquery:

           try
            {
                 SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;
                    AttachDbFilename=C:\USERS\OZ\DOCUMENTS\VISUAL STUDIO 2017\PROJECTS\SHILOVI2R\SHILOVI2R\PHONENUM.MDF;
                    Integrated Security=True;
                    Connect Timeout=30;
                    User Instance=True");

                conn.Open();
                SqlCommand cmd = new SqlCommand("insert into phonebook(שם,עיר,כתובת,מספר טלפון,אזור,מספר זיהוי,מחוז,נפה,דת)VALUES('" + rows[0] + "','" + rows[1] + "','" + rows[2] + "','" + rows[3] + "','" + rows[4] + "','" + rows[5] + "','" + rows[6] + "','" + rows[7] + "','" + rows[8] + "') ", conn);
                cmd.ExecuteNonQuery();
                MessageBox.Show("middle2", "SHILOVI2R", MessageBoxButtons.OK);
                Console.WriteLine("Inserting Data Successfully");
                conn.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show("dont_work", "SHILOVI2R", MessageBoxButtons.OK);
                Console.WriteLine("Exception Occre while creating table:" + e.Message + "\t" + e.GetType());
            }

视觉截图:

image screenshot

整个代码:

    private void button1_Click(object sender, EventArgs e)
    {
        string strFilePat = @"C:\Users\oz\Desktop\sql\backup\tabel3.csv";
        ConvertCSVtoDataTable(strFilePat, strFilePat);
    }

    public static DataTable ConvertCSVtoDataTable(string strFilePath, string conLocoldbString1)
    {
        MessageBox.Show("start", "SHILOV", MessageBoxButtons.OK);

        DataTable dt = new DataTable();

        using (StreamReader sr = new StreamReader(strFilePath))
        {
            string[] headers = sr.ReadLine().Split(',');
            foreach (string header in headers)
            {
                dt.Columns.Add(header);
            }
            while (!sr.EndOfStream)
            {

                string[] rows = sr.ReadLine().Split(',');
                DataRow dr = dt.NewRow();
                for (int i = 0; i < headers.Length; i++)
                {
                    dr[i] = rows[i];
                }
                try
                {
                    SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;
                        AttachDbFilename=C:\USERS\OZ\DOCUMENTS\VISUAL STUDIO 2017\PROJECTS\SHILOV\SHILOV\LOCALDBSHILOV.MDF;
                        Integrated Security=True;
                        Connect Timeout=30;
                        User Instance=True");

                    conn.Open();
                    SqlCommand cmd = new SqlCommand("insert into PhoneTable(??,???,?????,???? ?????,????,???? ?????,????,???,??)VALUES('" + rows[0] + "','" + rows[1] + "','" + rows[2] + "','" + rows[3] + "','" + rows[4] + "','" + rows[5] + "','" + rows[6] + "','" + rows[7] + "','" + rows[8] + "') ", conn);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("middle", "SHILOVI2R", MessageBoxButtons.OK);
                    Console.WriteLine("Inserting Data Successfully");
                    conn.Close();
                }
                catch (Exception e)
                {
                    MessageBox.Show("dont_work", "SHILOVI2R", MessageBoxButtons.OK);
                    Console.WriteLine("Exception Occre while creating table:" + e.Message + "\t" + e.GetType());
                }
                dt.Rows.Add(dr);
            }
        }
        MessageBox.Show("finish", "SHILOVI2R", MessageBoxButtons.OK);
        return dt;
    }
}

}

1 个答案:

答案 0 :(得分:0)

如果问题是连接字符串,请使用您附加的屏幕截图的属性选项卡中提供的连接字符串 This is what I'm talking about
将此作为连接字符串使用,还要记住将密码值从“*”替换为实际值。