1

Hi I'm having a problem finding the correct connection statement for my web-service to an sql-server database. I'm trying to retrieve data from my database to check a users login details.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

namespace BTC_Service
{
    public class UseDatabase
    {
        SqlConnection sqlConn;

        internal Boolean Connect()
        {
            try
            {
                sqlConn = new SqlConnection(@"Integrated Security=true; Initial Catalog=BTCFS_DataBase; Data Source=.\SQLEXPRESS;");
                sqlConn.Open();
                return true;
            }
            catch (SqlException ex)
            {
                return false;
            }
        }

        internal void DisconnectDatabase()
        {
            sqlConn.Close();
        }

        internal Boolean ExecuteCommand(String query)
        {
            try
            {
                SqlCommand cmd = sqlConn.CreateCommand();
                cmd.CommandText = query;
                cmd.ExecuteNonQuery();
                return true;
            }
            catch (SqlException ex)
            {
                return false;
            }
        }

        internal SqlDataReader ExecuteQuery(String query)
        {
            try
            {
                SqlCommand cmd = sqlConn.CreateCommand();
                cmd.CommandText = query;
                return cmd.ExecuteReader();
            }
            catch (SqlException ex)
            {
                return null;
            }
         }
    }
}

The database is created with sql-server 2008 and the path for it is:

C:\BTCFS_DataBase\db_BTDC_data.mdf

and the log file

C:\BTCFS_DataBase\db_BTDC_log.ldf

There is no password for the database and the code is as follows:

USE master
GO

create database db_BTCFC 
ON PRIMARY
(
    NAME =  'db_BTCFC_Data',
    FILENAME = 'c:\BTCFS_DataBase\db_BTDC_data.mdf',
    SIZE = 5MB,
    FILEGROWTH = 10%    
)
LOG ON
(
    NAME = 'db_BTFC_log',
    FILENAME = 'c:\BTCFS_DataBase\db_BTDC_log.ldf',
    SIZE = 5MB,
    FILEGROWTH = 10%
)
GO

Is there any suggestion to what I am doing wrong? Should I add the database to visual studio in a specific way? Or am i creating my database in the wrong way?

Thank you in advance.

0

2 Answers 2

1

The fact that you are connected using Integrated Security, means that your local user account on Windows should be authenticated on the SQL server instance which is hosted locally on your machine (evident by the "." in the Data Source, which refers to your local machine). It might be that the setup of your SQL server instance doesn't accommodate windows authentication. Check that your configuration allows for "mixed mode" authentication, i.e. either Windows authentication or username/password authentication...

Sign up to request clarification or add additional context in comments.

1 Comment

You may find this a useful link to your answer: connectionstrings.com/sql-server-2008
1

I found this statement to be more effective than the previous one:

sqlConn = new SqlConnection(@"Integrated Security=SSPI; Initial Catalog=BTCFS_DataBase; Data Source=localhost"); 

Thanks @Wolfish for the link.

1 Comment

You may wish to verify that this works from an external source, unless the db resides on your web server and the XML has the attribute Runat:="server" or similar. If the code runs in an instance of the site within the browser, you've got a major design flaw anyway :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.