C
C#9mo ago
Rugjrh

❔ DataBaseProblem

Hi Guys! I have this code when I try to make my db connection: SqlConnection con; public MainForm() { InitializeComponent(); con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True"); con.Open(); } But my app doesn't run. What's the problem? I'm a beginner in SQL.
28 Replies
Rugjrh
Rugjrh9mo ago
I try to learn every day new things..
Pobiega
Pobiega9mo ago
What do you mean by "my app doesn't run"?
Rugjrh
Rugjrh9mo ago
doesn't open I can't debug I can't to anything freez freeze*
Pobiega
Pobiega9mo ago
and if you set a breakpoint on the con.Open line actually, the problem is that you are doing this in your constructor
Rugjrh
Rugjrh9mo ago
if I put a comentary on con.Open it runs
Pobiega
Pobiega9mo ago
thats a terrible idea
Rugjrh
Rugjrh9mo ago
I don't know where to put it it's my first time when I create a db
Rugjrh
Rugjrh9mo ago
No description
Rugjrh
Rugjrh9mo ago
that's the error
Pobiega
Pobiega9mo ago
well that error is pretty clear it says it cant establish a connection, because your instance name is invalid
Rugjrh
Rugjrh9mo ago
i made a method for my conn and I open it in that method and now runs:) but now
Rugjrh
Rugjrh9mo ago
No description
Pobiega
Pobiega9mo ago
sounds like you didn't run the method :p
Rugjrh
Rugjrh9mo ago
:p yea ..
Pobiega
Pobiega9mo ago
I would highly suggest moving away from LocalDB thou sqlite is an excellent database and very good for beginners, as its file based
Rugjrh
Rugjrh9mo ago
yea I know but I have this thing for school
Pobiega
Pobiega9mo ago
so you literally have a single file with your app that contains the database, but otherwise behaves like a full SQL database
Rugjrh
Rugjrh9mo ago
and I can't move away for LocalDb
Pobiega
Pobiega9mo ago
rip
Rugjrh
Rugjrh9mo ago
for my personal apps I will do in sqlite
Pobiega
Pobiega9mo ago
I would run it on the forms Load event
Rugjrh
Rugjrh9mo ago
yea yea
Rugjrh
Rugjrh9mo ago
No description
Rugjrh
Rugjrh9mo ago
how do I remove the databse with same name? or what's the problem here? @Pobiega , sorry for @ you, but can u help me? I'm so freaked up with these things, because no one can help me... my teachers are useless
Pobiega
Pobiega9mo ago
Sorry, no idea about local db Maybe try clearing that MDF file?
Rugjrh
Rugjrh9mo ago
thanks and creating a new one? with another name?
Pobiega
Pobiega9mo ago
¯\_(ツ)_/¯ As said, I don't know how it works at all
Accord
Accord9mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.