adonet基礎(chǔ)示例分享(adonet連接數(shù)據(jù)庫(kù))
更新時(shí)間:2014年04月02日 10:05:56 作者:
這篇文章主要介紹了adonet基礎(chǔ)示例分享(adonet連接數(shù)據(jù)庫(kù)),需要的朋友可以參考下
adonet基礎(chǔ)示例分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
namespace ADONET基礎(chǔ) {
/// <summary>
/// Window1.xaml 的交互邏輯
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
////using() 自動(dòng)關(guān)閉數(shù)據(jù)庫(kù),回收資源。
////SqlConnection為建立和數(shù)據(jù)庫(kù)連接的對(duì)象。
//using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=myself;User Id=sa;Password=123;"))
//{
// conn.Open();//打開連接
// //通過連接,創(chuàng)建一個(gè)向數(shù)據(jù)庫(kù)發(fā)命令的對(duì)象SqlCommand
// using (SqlCommand cmd = conn.CreateCommand())//釋放資源。
// {
// //CommandText為要執(zhí)行的SQL的語(yǔ)句
// cmd.CommandText = "Insert into student(學(xué)號(hào),姓名) values(110,'張五')";
// //ExecuteNonQuery一般用來執(zhí)行Update Delete Insert 語(yǔ)句。
// cmd.ExecuteNonQuery();//執(zhí)行上面的SQL語(yǔ)句。
// }
//}
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=MyTest;User Id=sa;Password=123;"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
//cmd.CommandText = "select count(*) from student where 入學(xué)成績(jī)<570";
//cmd.CommandText = "select count(*) from student where 入學(xué)成績(jī)<570";
////ExecuteScalar一般用來執(zhí)行有且只有一行一列返回值的SQL語(yǔ)句。
//int i = (int)cmd.ExecuteScalar();
//MessageBox.Show(i+"人成績(jī)小于570分");
cmd.CommandText = "Insert into T_Student(Name,Age) output inserted.Id values('張顧',18);";
long i = (long)cmd.ExecuteScalar();
MessageBox.Show("Id為"+i);
}
}
MessageBox.Show("執(zhí)行完成");
}
}
}
復(fù)制代碼 代碼如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.SqlClient;
namespace ADONET基礎(chǔ) {
/// <summary>
/// Window1.xaml 的交互邏輯
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
////using() 自動(dòng)關(guān)閉數(shù)據(jù)庫(kù),回收資源。
////SqlConnection為建立和數(shù)據(jù)庫(kù)連接的對(duì)象。
//using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=myself;User Id=sa;Password=123;"))
//{
// conn.Open();//打開連接
// //通過連接,創(chuàng)建一個(gè)向數(shù)據(jù)庫(kù)發(fā)命令的對(duì)象SqlCommand
// using (SqlCommand cmd = conn.CreateCommand())//釋放資源。
// {
// //CommandText為要執(zhí)行的SQL的語(yǔ)句
// cmd.CommandText = "Insert into student(學(xué)號(hào),姓名) values(110,'張五')";
// //ExecuteNonQuery一般用來執(zhí)行Update Delete Insert 語(yǔ)句。
// cmd.ExecuteNonQuery();//執(zhí)行上面的SQL語(yǔ)句。
// }
//}
using (SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=MyTest;User Id=sa;Password=123;"))
{
conn.Open();
using (SqlCommand cmd = conn.CreateCommand())
{
//cmd.CommandText = "select count(*) from student where 入學(xué)成績(jī)<570";
//cmd.CommandText = "select count(*) from student where 入學(xué)成績(jī)<570";
////ExecuteScalar一般用來執(zhí)行有且只有一行一列返回值的SQL語(yǔ)句。
//int i = (int)cmd.ExecuteScalar();
//MessageBox.Show(i+"人成績(jī)小于570分");
cmd.CommandText = "Insert into T_Student(Name,Age) output inserted.Id values('張顧',18);";
long i = (long)cmd.ExecuteScalar();
MessageBox.Show("Id為"+i);
}
}
MessageBox.Show("執(zhí)行完成");
}
}
}
相關(guān)文章
C# 實(shí)現(xiàn)拖拉控件改變位置與大小的方法
下面小編就為大家分享一篇C# 實(shí)現(xiàn)拖拉控件改變位置與大小的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01C#創(chuàng)建Windows服務(wù)與服務(wù)的安裝、卸載
這篇文章介紹了C#創(chuàng)建Windows服務(wù)與服務(wù)的安裝、卸載,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02c# xml轉(zhuǎn)word的實(shí)現(xiàn)示例
這篇文章主要介紹了c# xml轉(zhuǎn)word的實(shí)現(xiàn)示例,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-04-04C#根據(jù)權(quán)重抽取隨機(jī)數(shù)
最近在開發(fā)過程中遇到一個(gè)需要做帶權(quán)隨機(jī)的處理,本文主要介紹了C#根據(jù)權(quán)重抽取隨機(jī)數(shù),具有一定的參考價(jià)值,感興趣的可以了解一下2024-02-02C#自動(dòng)給文章關(guān)鍵字加鏈接實(shí)現(xiàn)代碼
這篇文章主要介紹了C#自動(dòng)給文章關(guān)鍵字加鏈接實(shí)現(xiàn)代碼,有需要的朋友可以參考一下2013-12-12