Baza SQL Server

0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Data;
namespace Csharp_Project
{
    class DB
    {
        SqlConnection connection;

        public DB()
        {
            connection = new SqlConnection(@"Data Source=RADEK-PC\SQLEXPRESS;Integrated Security=False;User ID=sa;Password=lewandowski");
        }

        public void openConnection()
        {
            if (connection.State != ConnectionState.Open)
            {
                connection.Open();
            }
        }

        public void closeConnection()
        {
            if (connection.State != ConnectionState.Closed)
            {
                connection.Close();
            }
        }

        public DataTable getData(string procedureName, SqlParameter[] procedureParams)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = procedureName;
            command.Connection = connection;
            if (procedureParams != null)
            {
                for (int i = 0; i < procedureParams.Length; i++)
                {
                    command.Parameters.Add(procedureParams[i]);
                }
            }
            
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataTable table = new DataTable();
                adapter.Fill(table);
            
            return table;
            
        }

        public void setData(string procedureName, SqlParameter[] procedureParams)
        {
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = procedureName;
            command.Connection = connection;
            if (procedureParams != null)
            {
                command.Parameters.AddRange(procedureParams);
            }

            command.ExecuteNonQuery();
        }

    }
}

SqlDataAdapter adapter = new SqlDataAdapter(command);
                DataTable table = new DataTable();
                adapter.Fill(table);
            
            return table;

tutaj wywala mi błąd i juz nie mam pojecia czemu a dokladnie brzmi on tak : An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: Login failed for user 'sa'. Proszę was o pomoc.

0

A jeżeli w connection stringu wyrzucisz pola user id i password, a Integrated Security zmienisz na true to działa?

0

Dzieki ale juz ogarnałem po prostu nie mozna podawac ściezki do adresu servera bezposrednio do bazy bo chyba nie bedzie wtedy mogla byc uzyta w kilku miejscach .

0

connection = new SqlConnection(@"Data Source=RADEK-PC\SQLEXPRESS;Integrated Security=False;User ID=sa;Password=lewandowski"); zmieniłem na
connection = new SqlConnection(@"Server= RADEK-PC\SQLEXPRESS; Database = MY_DB; Integrated Security = true");

0
EntityPamerano napisał(a):

A jeżeli w connection stringu wyrzucisz pola user id i password, a Integrated Security zmienisz na true to działa?

i własnie chyba o to chodziło :)

0

Możliwości sa 2:

  1. Podałeś złe hasło dla sa
  2. Serwer nie ma wlaczonej autoryzacji SQL
0

A to nie jest tak że nie podając nazwy bazy danych, sqlconnection próbuje się łączyć do domyślnej bazy danych a dla SA ona po prostu nie jest ustawiona ? Albo nie masz włączonego Mixed Authentication - przez co nie można się logować na SA, a tak jest w domyślnej konfiguracji przy instalacji SQL Server'a.

0

@Slepiec: Jeżeli nie ma domyslnej bazy ustawionej, to "wybierana" jest baza master

1 użytkowników online, w tym zalogowanych: 0, gości: 1