1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > visual studio连接SQL Server数据库 增删改查详细教程(C#代码)

visual studio连接SQL Server数据库 增删改查详细教程(C#代码)

时间:2018-11-15 02:12:32

相关推荐

visual studio连接SQL Server数据库 增删改查详细教程(C#代码)

visual studio连接SQL Server数据库,增删改查详细教程(C#代码)

工具:

1.Visual Studio

2.SQL Server数据库(我使用的)

操作:

1.打开SQL Server,打开后会看到数据库的初始链接界面。

2…复制图中的“服务器名称”,然后点击“连接”,进入数据库。

3.打开vs,创建好自己要用的项目,我写的项目名称叫做:‘finnal_test’

4.工具->连接到数据库->在服务器名里面,粘贴复制的服务器名

5.在下面选择自己要连接的数据库名称(也可以手动输入,我连接的是我自己创建的数据库:shaohui),确定

6.打开“服务器资源管理器”,点击“表”可以看到数据库里面创建的数据表

连接代码:

完成上述操作后只是把数据库添加到了vs里,要想在项目里对数据库进行编辑,还需要写一些代码。

1.打开自己的项目,选择项目->添加类

类名自己起,我这里是SQLServerDataBase

2.打开类文件,写入以下代码。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Data.SqlClient;using System.Threading.Tasks;//必要的命名空间namespace finnal_test{class SQLServerDataBase {//MySqlCon部分,每个人不相同,后面我会进行说明,下面的是我计算机相应的配置 private string MySqlCon = "Data Source=DESKTOP-8LDERGD\\SQLEXPRESS;Initial Catalog = shaohui; Integrated Security = True"; public DataTable ExecuteQuery(string sqlStr)//用于查询;其实是相当于提供一个可以传参的函数,到时候写一个sql语句,存在string里,传给这个函数,就会自动执行。 {SqlConnection con = new SqlConnection(MySqlCon); con.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; mandType = CommandType.Text; mandText = sqlStr; DataTable dt = new DataTable(); SqlDataAdapter msda = new SqlDataAdapter(cmd); msda.Fill(dt); con.Close(); return dt; } public int ExecuteUpdate(string sqlStr)//用于增删改; {SqlConnection con = new SqlConnection(@MySqlCon);con.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = con; mandType = CommandType.Text; mandText = sqlStr; int iud = 0; iud = cmd.ExecuteNonQuery(); con.Close(); return iud; } }}

3.修改代码里的MySqlCon,这一步用来连接到数据库,至关重要。

在“服务器资源管理”处选中数据库,然后可以在“属性”窗口找到“连接字符串”,复制其内容,赋给MySqlCon。

比如我修改后是:MySqlCon = “Data Source=DESKTOP-8LDERGD\SQLEXPRESS; Initial Catalog = shaohui; Integrated Security = True”;

完成这些操作后,就可以在form里写代码来修改数据库了。

VS 连接SQL server数据库方法

建立SteelLadleTrackSystem的窗口.net项目在服务器资源管理器中右击数据连接,键入如下信息,其中密码是安装Sqlserver 时为sa键入的密码 WUnn4*0

plusoft_test是建立的数据库。所用.sql见后文。

服务器名,是安装 Sql Server时确定的。通过运行Microsoft Sql Server Management Studio 18可以看到这个名字。

填写无误后测试是正确的。

参考上面内容修改如下:

vs新建一个Windows窗口应用程序,界面布局如下:

` using System;using System.Collections.Generic;using ponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms; //new addusing System.Data.SqlClient; namespace SteelLadleTraceSystem{public partial class Form1 : Form {public Form1() {InitializeComponent(); }SqlConnection myconnection; private void Connect_Click(object sender, EventArgs e) {try {//定义一个sqlserver数据库连接对象myconnection = new SqlConnection("Data Source = DESKTOP-6GUAH20\\SQLEXPRESS;Initial Catalog = plusoft_test; User ID = sa; Password=***");说明:上面Password后的“***”应替换为 数据库访问密码 myconnection.Open();//打开数据库label1.Text = "数据库连接成功!"; } catch (Exception ee) {MessageBox.Show("数据库连接失败!" + ee.ToString()); } } private void InsertToDatabase_Click(object sender, EventArgs e) {try {//插入数据string strSQL1 = "insert into t_position(id,name,dept_id) values('rs3','人事实习生','rs')"; SqlDataAdapter objDataAdpter = new SqlDataAdapter(); SqlCommand thisCommand = new SqlCommand(strSQL1, myconnection); thisCommand.ExecuteNonQuery(); //以下代码为显示数据表string strSQL2 = "select * from t_position"; SqlDataAdapter objDataAdpter1 = new SqlDataAdapter(); objDataAdpter1.SelectCommand = new SqlCommand(strSQL2, myconnection); DataSet ds = new DataSet(); objDataAdpter1.Fill(ds, "Table_3_ceshi"); dataGridView1.DataSource = ds.Tables[0];} catch (Exception ee) {MessageBox.Show("插入数据失败!" + ee.ToString()); } } private void QuerryAll_Click(object sender, EventArgs e) {try {string SQL = "select * from t_position";SqlDataAdapter objDataAdpter = new SqlDataAdapter();objDataAdpter.SelectCommand = new SqlCommand(SQL, myconnection);DataSet ds = new DataSet();objDataAdpter.Fill(ds, "t_position");dataGridView1.DataSource = ds.Tables[0];} catch (Exception ee) {} } private void ModifyFeildValue_Click(object sender, EventArgs e) {try {//修改数据表内容 string strSQL1 = "update t_position set name='人事实习班长' where id='rs3'"; SqlCommand thisCommand = new SqlCommand(strSQL1, myconnection);thisCommand.ExecuteNonQuery();//显示数据表 string strSQL2 = "select * from t_position";SqlDataAdapter objDataAdpter1 = new SqlDataAdapter();objDataAdpter1.SelectCommand = new SqlCommand(strSQL2, myconnection);DataSet ds = new DataSet();objDataAdpter1.Fill(ds, "t_position");dataGridView1.DataSource = ds.Tables[0];} catch (Exception ee) {MessageBox.Show("更新数据失败!" + ee.ToString());} } private void DeleteFeildValue_Click(object sender, EventArgs e) {try {//删除数据表某条记录 string strSQL1 = "delete from t_position where name='人事实习班长'"; SqlCommand thisCommand = new SqlCommand(strSQL1, myconnection); thisCommand.ExecuteNonQuery(); //显示数据表 string strSQL2 = "select * from t_position";SqlDataAdapter objDataAdpter1 = new SqlDataAdapter();objDataAdpter1.SelectCommand = new SqlCommand(strSQL2, myconnection);DataSet ds = new DataSet();objDataAdpter1.Fill(ds, "t_position");dataGridView1.DataSource = ds.Tables[0]; } catch (Exception ee) {MessageBox.Show("删除数据失败!" + ee.ToString());} } //关闭连接函数 public string DisConnectFunc() {string Result; try {myconnection.Close();Result = "数据连接已断开!"; } catch (Exception e) {MessageBox.Show("数据库断开失败!" + e.ToString()); Result = "连接成功!"; } return Result; } private void DisConnect_Click(object sender, EventArgs e) {label1.Text = DisConnectFunc(); } }}`

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。