Откройте вкладку Server Explorer, кликните правой кнопкой мыши на Data Connections и нажмите Add Connection.
Добавьте соединение. Во вкладке Property нам понадобятся свойства:
- Name
- Provider
- Connection String
Откройте файл App.config и добавьте фрагмент кода.
<connectionStrings> <add name="" providerName="" connectionString="" /> </connectionStrings>
Из вкладки Property скопируйте значения Name, Provider, Connection String и вставьте в значения соответствующих атрибутов.
App.config
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <!--Добвавить--> <connectionStrings> <add name="abc" providerName=".NET Framework Data Provider for SQL Server" connectionString="Data Source=BISEM\MSSQLSERVER2012;Initial Catalog=abc;Integrated Security=True"/> </connectionStrings> </configuration>
<?xml version="1.0" encoding="utf-8" ?> <configuration> <startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup> <!--Добвавить--> <connectionStrings> <add name="abc" providerName=".NET Framework Data Provider for SQL Server" connectionString="Data Source=BISEM\MSSQLSERVER2012;Initial Catalog=abc;Integrated Security=True"/> </connectionStrings> </configuration>
Form1.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; //добавить ссылку System.Configuration.dll //подключить пространство имен using System.Configuration; namespace _0083 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //кнопка Add private void button1_Click(object sender, EventArgs e) { //строка соединения ConnectionStringSettings stringConnect = ConfigurationManager.ConnectionStrings["abc"]; string sql = "INSERT INTO Avto VALUES('"+textBox1.Text+"', '"+textBox2.Text+"')"; using(SqlConnection connect = new SqlConnection(stringConnect.ConnectionString)) { try { connect.Open(); SqlCommand command = new SqlCommand(sql, connect); int x = command.ExecuteNonQuery(); MessageBox.Show(x.ToString() + " Record added"); } catch { MessageBox.Show("Error connection with a database"); } } } } }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.Data.SqlClient; //добавить ссылку System.Configuration.dll //подключить пространство имен using System.Configuration; namespace _0083 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //кнопка Add private void button1_Click(object sender, EventArgs e) { //строка соединения ConnectionStringSettings stringConnect = ConfigurationManager.ConnectionStrings["abc"]; string sql = "INSERT INTO Avto VALUES('"+textBox1.Text+"', '"+textBox2.Text+"')"; using(SqlConnection connect = new SqlConnection(stringConnect.ConnectionString)) { try { connect.Open(); SqlCommand command = new SqlCommand(sql, connect); int x = command.ExecuteNonQuery(); MessageBox.Show(x.ToString() + " Record added"); } catch { MessageBox.Show("Error connection with a database"); } } } } }