Solved

Accessing a MSSQL 2008 DB in C#

Posted on 2011-02-28
8
616 Views
Last Modified: 2012-05-11
I've been Installing/configuring MSSQL with TCPIP and accessing it just fine in C#.  But I've seen references that make me believe that it's possible to access a MSSQL .mdf file directly and not using named pipes or TCPIP.  Is this true?

It's come more to light while working on my install routing using innosetup.  In one of the examples I was referring to it appears that innosetup creates the DB outside of mssql 2008 express after mssql 2008r2 express was installed.

Then in another part I saw some code prototyping that appeared to access the MDF file directly.  Is this still using MSSQL 2008 just not the server or what?
0
Comment
Question by:sej69
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 8

Expert Comment

by:WesWilson
ID: 35000916
I would recommend continuing to use a standard connection string, not going after the .MDF file directly. I think you will find that more standard and more flexible, especially if you are developing an application that will be hosted eventually on a separate machine from the DB host machine.
0
 

Author Comment

by:sej69
ID: 35001290
I agree with you 100%; I'm just trying to get this straight in my head with different references to accessing the MDF file.  Is it possible and/or is it actually using MSSQL at this point or something in C# to access the MDF?
0
 
LVL 20

Expert Comment

by:Marten Rune
ID: 35005560
Weather it's possible or not is not the point.

The design of a database server (MS SQL, ORACLE, ySQL, DB2 etc) is to write to a log file first, and then have commit/rollback transactions. This is hotwired if you access the MDF file directly. I would STRONGLY argue against it.

There will not be any performancegain, and every other parameter is worse. Simply put, don't go that route!

//Marten
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35005745
yes its possible to access the mdf file directly.you can use the connection string like:-

Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Databases\test.mdf";User ID=sa;Password=123;Connect Timeout=30;User Instance=True
0
 
LVL 9

Expert Comment

by:mayank_joshi
ID: 35005812
0
 
LVL 20

Expert Comment

by:Marten Rune
ID: 35005998
The following:

Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Databases\test.mdf";User ID=sa;Password=123;Connect Timeout=30;User Instance=True    

Does not access the mdf file DIRECTLY. It accesses it through sql server (express). I e it's just another way to determine the database name, by sending the parameter of the mdf file to the server.

//Marten
0
 
LVL 9

Accepted Solution

by:
mayank_joshi earned 500 total points
ID: 35006322
Data Source=.\SQLEXPRESS;AttachDbFilename="C:\Databases\test.mdf";User ID=sa;Password=123;Connect Timeout=30;User Instance=True


martenrune,
i think it  does not use TCP/IP or Named Pipes and so it fullfills the requirement.
0
 

Author Closing Comment

by:sej69
ID: 35013911
Thanks, I was just trying to get my head around this in some of the documentation I was reading out there.  I'm going to stick with TCPIP connections but at least now I understand that the docs I was reading was referring direct access to the db file and can translate that into what I need.
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question