Witam, mam pewien problem, mianowicie dostałem do zrobienia na pracę zaliczeniową bazę danych filmów w Windows Forms i juz na początku mam pod górkę, przechodząc do sedna, przy próbie uruchomienia programu mam błąd "dbnetlib connectionopen (connect()). serwer sql nie istnieje"
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
//to jest porzebne, aby wyświetlić dane w tabelce
private BindingSource bindingSource1 = new BindingSource();
//string strConnection =
// @"Provider=SQLOLEDB.1;Integrated Security=SSPI;Data Source=MACHINAZAGLADY4\SQLEXPRESS;Initial Catalog=test";
string strConnection =
@"Provider=SQLOLEDB.1;Password=tajnehaslo;Persist Security Info=True;"+
"User ID=test;Data Source=MACHINAZAGLADY4\SQLEXPRESS;Initial Catalog=test";
//tworzy tabelkę z danymi - dodaje kolumny itp.
public void tableInit()
{
dataGridView1.AutoGenerateColumns = false;
dataGridView1.AutoSize = false;
dataGridView1.DataSource = bindingSource1;
//nowa kolumna
DataGridViewColumn columnId = new DataGridViewTextBoxColumn();
columnId.DataPropertyName = "Id";
columnId.Name = "ID";
dataGridView1.Columns.Add(columnId);
//nowa kolumna
DataGridViewColumn columnNazwa = new DataGridViewTextBoxColumn();
columnNazwa.DataPropertyName = "Nazwa";
columnNazwa.Name = "Nazwa";
dataGridView1.Columns.Add(columnNazwa);
//nowa kolumna
DataGridViewColumn columnIlosc = new DataGridViewTextBoxColumn();
columnIlosc.DataPropertyName = "Ilosc";
columnIlosc.Name = "Ilość";
dataGridView1.Columns.Add(columnIlosc);
//dodajemy testowy obiekt
bindingSource1.Add(new EncjaT1(0, "TEST", 123));
}
//łączy się z bazą i wypełnia tabelkę danymi
public void refreshTable()
{
OleDbConnection connection = null;
OleDbDataReader reader = null;
try
{
//tworzymy połączenie
connection = new OleDbConnection(this.strConnection);
//łączymy się
connection.Open();
//tworzymy nowe polecenie
OleDbCommand command = new OleDbCommand("SELECT id, nazwa, ilosc FROM T1", connection);
reader = command.ExecuteReader();
//wyczyśmy stare dane
bindingSource1.Clear();
//czytamy dane
while (reader.Read())
{
int id = reader.GetInt32(0);
string nazwa = reader.GetString(1);
int ilosc = reader.GetInt32(2);
bindingSource1.Add(new EncjaT1(id, nazwa, ilosc));
}
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally
{
connection.Close();
reader.Close();
}
}
public void addNewtoT1(){
OleDbConnection connection = null;
string nazwa = textBox1.Text;
int ilosc = -1;
try
{
ilosc = int.Parse(textBox2.Text);
//nowe połączenie
connection = new OleDbConnection(this.strConnection);
connection.Open();
//procedura
OleDbCommand command = new OleDbCommand("sp_add_to_t1", connection);
command.CommandType = CommandType.StoredProcedure;
//parametry
command.Parameters.Add("@nazwa", OleDbType.VarChar, 256);
command.Parameters.Add("@ilosc", OleDbType.Integer);
command.Parameters.Add("@lastId", OleDbType.SmallInt);
//wartości
command.Parameters["@nazwa"].Value = nazwa;
command.Parameters["@ilosc"].Value = ilosc;
command.Parameters["@lastId"].Direction = ParameterDirection.Output; //kierunek
command.ExecuteNonQuery();
//pobieramy zwróconą wartość
string lastId = command.Parameters["@lastId"].Value.ToString();
MessageBox.Show(lastId, "Dodano", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error",
MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
public Form1()
{
InitializeComponent();
//wywołujemy stworzenie struktury tabelki
tableInit();
//wypełniamy tabelkę
refreshTable();
}
private void button1_Click(object sender, EventArgs e)
{
addNewtoT1();
refreshTable();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}
public class EncjaT1
{
public string Nazwa { get; set; }
public int Id { get; set; }
public int Ilosc { get; set; }
public EncjaT1()
{
}
public EncjaT1(int id, string nazwa, int ilosc)
{
this.Nazwa = nazwa;
this.Id = id;
this.Ilosc = ilosc;
}
}
}