Creating database in SQL 2000 and connecting it in .Net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Parul Vinod
    New Member
    • Aug 2011
    • 36

    Creating database in SQL 2000 and connecting it in .Net

    Hello All,
    I am unable to create a new database in SQL 2000. Please tell me how to do that. Once the database is created i want to have a connection string to connect SQL in .Net in C#
  • Paul Johnson
    New Member
    • Oct 2010
    • 97

    #2
    If you're using VS2005/08/10 then it's easy to create and populate the database - the wizard is the way to go. The hard part is connecting to the database. This is how I do it for SQL2008

    Code:
    SqlConnection sql = new SqlConnection();
    sql.ConnectionString = "Data Source=.\\SQLExpress;" + "AttachDbFilename=<your filename with the path>;" + "Integrated Security=True;" + "User Instance=True;";
    using (SqlCommand sqlc = sql.CreateCommand())
    {
      // sql stuff
      sql.Open();
      sqlc.ExecuteNonQuery(); // or ExecuteQuery if it returns something
      sql.Close();
    }
    An example of the filename bit

    Code:
    C:\\Users\\paul\\Documents\\visual studio 2010\\Projects\\app_name\\app_name\\App_Data\\database_name.mdf
    Probably lots of other ways to do it...

    Comment

    • Parul Vinod
      New Member
      • Aug 2011
      • 36

      #3
      Thanks Paul.

      I could solve the problem by different approach.

      Select Enterprise manager in case of SQL Server 2000 or SQL Server Management Studio in case of SQL Server 2005, then by right clicking on database, one can create new database.

      connection string is as follows:
      Code:
      SqlConnection cn;
      string str;
      str="Data Source=PC Name; Initial Catalog=Database Name;" + 
      "Integrated Security=True";
      cn=new SqlConnection(str);

      Comment

      • Paul Johnson
        New Member
        • Oct 2010
        • 97

        #4
        Thanks for the tip off :)

        Comment

        • Parul Vinod
          New Member
          • Aug 2011
          • 36

          #5
          I am getting an error while opening the connection:
          Code:
          cn=new SqlConnection(str)
          cn.open();
          :
          Login failed for user '(null)'. Reason: Not associated with a trusted SQL Server connection

          Comment

          • Paul Johnson
            New Member
            • Oct 2010
            • 97

            #6
            Try the connection string I used (obviously, modify it first)

            Comment

            • Parul Vinod
              New Member
              • Aug 2011
              • 36

              #7
              Its not working, someone please help me with it.

              Comment

              • Paul Johnson
                New Member
                • Oct 2010
                • 97

                #8
                What are you using for the connection string and more over, how have you got MSSQL configured? Do you need a logon or are you using the default of windows logon?

                Comment

                • Parul Vinod
                  New Member
                  • Aug 2011
                  • 36

                  #9
                  I am using default windows login and connection string is same as i have written in earlier post.

                  Comment

                  • Paul Johnson
                    New Member
                    • Oct 2010
                    • 97

                    #10
                    Try using the connection string I posted. MSSQL can be a pain at times and I know that for all of my uses, it works.

                    Comment

                    • Parul Vinod
                      New Member
                      • Aug 2011
                      • 36

                      #11
                      Its working now, earlier i had forgotten to give semicolon after database name.

                      Comment

                      Working...