[Visual studio] coupling to a database - Helena61/-bookshelf GitHub Wiki
Create a project
Go to 'Solution Explorer' (window on the right)
- Resources.resx
- Settings.setting - configuration, i.e 'database-connection' strings
- 'References'-Folder
- App.config -> xml-file
- Program.cs ->
- Form1.cs -> first form
Create the database
- right click on the project
- Add 'New Item' -> choose 'Data' -> choose 'Service Based Database' ( that is a 'local DB Engine') -> give it a name
- 'UdemyBooks.mdf' > Click 'Add'
- see the 'Solution Explorer' -> UdemyBooks.mdf is visible. -> Double-Click
- see the 'Server Explorer' (window on the left) ( with 'tables', 'views', 'stored procedures' etc)
- -> right-click on 'Tables' -> Create your first table.
Connection - From
public partial class frmTitle : Form
{
System.Data.SqlClient.SqlConnection sqlConnection;
public frmTitle()
{
InitializeComponent();
}
private void frmTitle_Load(object sender, EventArgs e)
{
// Connection object, jag vet inte om detta fungerar - Udemy använder en Accessdatabas. Section 3, Lesson 21
var connString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\UdemyBooks.mdf;Integrated Security=True";
sqlConnection = new System.Data.SqlClient.SqlConnection();
sqlConnection.ConnectionString = connString;
sqlConnection.Open();
txtCon.Text = sqlConnection.State.ToString();
Thread.Sleep(10000);
sqlConnection.Close();
txtCon.Text = sqlConnection.State.ToString();
/* Thread.Sleep(10000);
sqlConnection.Dispose();
txtCon.Text = sqlConnection.State.ToString();
*/
}
}
Count
Variant 1
SqlCommand titlesCountCommand;
// test, hur visar man count ?
titlesCountCommand = new SqlCommand("Select count(*) from titles", sqlConnection);
Int32 count = (Int32)titlesCountCommand.ExecuteScalar();
txtCountTitles.Text = Convert.ToString(count);
Variant 2
titlesManager = (CurrencyManager)BindingContext[titlesTable];
txtCountTitles.Text = Convert.ToString(titlesManager.Count);
Connection - Form Again ( With Navigation)
public partial class frmTitle : Form
{
SqlConnection sqlConnection;
SqlCommand titlesCommand;
SqlDataAdapter titlesAdapter;
DataTable titlesTable;
// to navigate
CurrencyManager titlesManager;
// test
SqlCommand titlesCountCommand;
public frmTitle()
{
InitializeComponent();
}
private void frmTitle_Load(object sender, EventArgs e)
{
// Connection object, jag vet inte om detta fungerar - Udemy använder en Accessdatabas. Section 3, Lesson 21
var connString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\UdemyBooks.mdf;Integrated Security=True";
sqlConnection = new SqlConnection();
sqlConnection.ConnectionString = connString;
sqlConnection.Open();
txtCon.Text = sqlConnection.State.ToString(); // textbox is hidden now
titlesCommand = new SqlCommand("Select * from titles", sqlConnection);
// titlesAdapter = new SqlDataAdapter(titlesCommand);
titlesAdapter = new SqlDataAdapter();
titlesAdapter.SelectCommand = titlesCommand;
titlesTable = new DataTable();
titlesAdapter.Fill(titlesTable);
// bind the controls. "title' is the column in the 'titles'-table
txtTitle.DataBindings.Add("Text", titlesTable, "title");
// txtISB
txtISBN.DataBindings.Add("Text", titlesTable, "isbn");
txtPubId.DataBindings.Add("Text", titlesTable, "pubId");
// establish currency manager ; an array that one can navigate-through
titlesManager = (CurrencyManager)BindingContext[titlesTable];
txtCountTitles.Text = Convert.ToString(titlesManager.Count);
/*
titlesCountAdapter = new SqlDataAdapter();
titlesCountAdapter.SelectCommand = titlesCountCommand;
titlesCountAdapter.Fill(titlesTable);
*/
sqlConnection.Close();
sqlConnection.Dispose();
titlesAdapter.Dispose();
titlesTable.Dispose();
/*
Thread.Sleep(10000);
sqlConnection.Close();
txtCon.Text = sqlConnection.State.ToString();
*/
/* Thread.Sleep(10000);
sqlConnection.Dispose();
txtCon.Text = sqlConnection.State.ToString();
*/
}
private void btnFirst_Click(object sender, EventArgs e)
{
titlesManager.Position = 0;
}
private void btnPrevious_Click(object sender, EventArgs e)
{
titlesManager.Position--; // decrease by one
}
private void bntNext_Click(object sender, EventArgs e)
{
titlesManager.Position++; // increase by one
}
private void btnLast_Click(object sender, EventArgs e)
{
titlesManager.Position = titlesManager.Count - 1;
}
}