8 Replies - 190 Views - Last Post: 03 June 2019 - 10:56 AM Rate Topic: -----

#1 BenignDesign   User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 7977
  • View blog
  • Posts: 12,402
  • Joined: 28-September 07

LocalDB Connection Issue

Posted 03 June 2019 - 08:26 AM

I'm attempting to use this to connect to the LocalDB in Visual Studio:
@{
   var db = Database.Open("Server=(localdb)\\MSSQLLocalDB;Database=P2T;Trusted_Connection=True;");
   var selectQueryString = "INSERT INTO [dbo].[VideoCapture] (Duration, FarthestSecondsViewed, VideoId) VALUES ('2019-05-30 00:09:11', 55, 296)";
   var data = db.Query(selectQueryString);
   db.Close();
}



But I'm getting this error:

Quote

InvalidOperationException: Connection string "Server=(localdb)\MSSQLLocalDB;Database=P2T;Trusted_Connection=True;" was not found.


Any suggestions on how to remedy this issue?

Thank you all!

This post has been edited by BenignDesign: 03 June 2019 - 08:27 AM


Is This A Good Question/Topic? 0
  • +

Replies To: LocalDB Connection Issue

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15106
  • View blog
  • Posts: 60,395
  • Joined: 12-June 08

Re: LocalDB Connection Issue

Posted 03 June 2019 - 08:35 AM

In VS.. View -> sql server object explorer.

Drill into your local db -> right click -> properties.. and it will give you the connection string.

I believe you have one too many forward slashes.

Quote

Server=(localdb)\\MSSQLLocalDB;Database=P2T;Trusted_Connection=True;

Was This Post Helpful? 0
  • +
  • -

#3 BenignDesign   User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 7977
  • View blog
  • Posts: 12,402
  • Joined: 28-September 07

Re: LocalDB Connection Issue

Posted 03 June 2019 - 09:02 AM

Hmm... found the full connection string under properties. Thanks for that.

However, using the given string results in the same error. But removing one slash results in an "Unrecognized Escape Sequence" error.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15106
  • View blog
  • Posts: 60,395
  • Joined: 12-June 08

Re: LocalDB Connection Issue

Posted 03 June 2019 - 09:09 AM

Hmm.. so not the connection string.

Are you going the 'entity framework' route, or just what ever works?
Was This Post Helpful? 0
  • +
  • -

#5 BenignDesign   User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 7977
  • View blog
  • Posts: 12,402
  • Joined: 28-September 07

Re: LocalDB Connection Issue

Posted 03 June 2019 - 09:13 AM

I wish I could say I was going a specific route, but my goal at the moment is just to make it work.
Was This Post Helpful? 0
  • +
  • -

#6 DarenR   User is offline

  • D.I.C Lover

Reputation: 616
  • View blog
  • Posts: 4,090
  • Joined: 12-January 10

Re: LocalDB Connection Issue

Posted 03 June 2019 - 09:22 AM

have you tried placing your connection string as follows in the app settings


<add name:"some dbname" connectionstring= "Data Source=.\Server;Initial Catalog=P2T"
etc
Was This Post Helpful? 0
  • +
  • -

#7 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15106
  • View blog
  • Posts: 60,395
  • Joined: 12-June 08

Re: LocalDB Connection Issue

Posted 03 June 2019 - 09:44 AM

Gotcha. I still roll the old school route of getting a dataset and then converting out to a collection of some class object.


            System.Data.DataSet ds;
            string sql = string.Empty;

            List<string> myList;

            try
            {
                using (SqlConnection con = new SqlConnection(""))//1.0  AppSettings.Get("db")
                {
                    con.Open();

                    sql = "";//2.0  SQL statement.
                    using (SqlDataAdapter adapt = new System.Data.SqlClient.SqlDataAdapter(sql, con))
                    {
                        adapt.SelectCommand.Parameters.Add("", SqlDbType.VarChar).Value = "";

                        ds = new DataSet();

                        adapt.Fill(ds);
                    }
                }

                if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                {
                    myList = new List<string>();

                    foreach (DataRow tempRow in ds.Tables[0].Rows)
                    {
                        myList.Add((string)tempRow[""]);
                    }
                }

            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

Was This Post Helpful? 0
  • +
  • -

#8 BenignDesign   User is offline

  • holy shitin shishkebobs
  • member icon




Reputation: 7977
  • View blog
  • Posts: 12,402
  • Joined: 28-September 07

Re: LocalDB Connection Issue

Posted 03 June 2019 - 10:54 AM

modi, you beautiful motherfucker, it works! :bananaman:
Was This Post Helpful? 0
  • +
  • -

#9 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15106
  • View blog
  • Posts: 60,395
  • Joined: 12-June 08

Re: LocalDB Connection Issue

Posted 03 June 2019 - 10:56 AM

I have another snippet I use for 'ExecuteNonQuery'.

I should probably dump them in the C# snippet area.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1