您现在的位置是:网站首页> 编程资料编程资料
SQLServer 在Visual Studio的2种连接方法_实用技巧_
2023-05-24
347人已围观
简介 SQLServer 在Visual Studio的2种连接方法_实用技巧_
一、Sql Server 在Visual Studio的连接有两种方法:
(1)本地计算机连接;
string s = "Data Source=计算机名称;initial Catalog=数据库名称;integrated Security=True";
(2)windows身份验证方式连接;
string cc="Data Source = 计算机名称; Initial Catalog = 数据库名称; User ID = sa; Password = 你的密码";
二、在Visual Studio中使用:
例1:查询数据库中的数据并且显示出来
string s = "Data Source=计算机名称;Initial Catalog=数据库名称;Integrated Security=True"; //此处使用本地计算机连接方式
SqlConnection conn = new SqlConnection(s); //创建连接
conn.Open(); //打开连接
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from T_User"; //使用命令
SqlDataAdapter adapter=new SqlDataAdapter(cmd);
DataTable dt=new DataTable();
adapter.Fill(dt);
conn.Dispose(); //释放所以资源
cmd.Dispose();
conn.Close(); //关闭连接
string realname="";
string username="";
string mobile="";
string address="";
for (int i=0;i{
realname=dt.Rows[i][3].ToString();
username=dt.Rows[i][1].ToString();
mobile=dt.Rows[i][4].ToString();
address=dt.Rows[i][5].ToString();
Console.WriteLine("姓名为{0},用户名为{1},手机为{2},地址为{3}", realname, username, mobile, address);
}
Console.ReadKey();
例2:删除表中数据
string cc="Data Source = 计算机名称; Initial Catalog = 数据库名称; User ID = sa; Password = 你的密码"; //使用windows身份验证
SqlConnection conn = new SqlConnection(s);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "delete from T_User where Id=5";
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
Console.WriteLine("删除成功");
Console.ReadKey();
例3:修改表中数据
string s = "Data Source=计算机名称;initial Catalog=数据库名称;integrated Security=True";
SqlConnection conn = new SqlConnection(s);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "update T_User set Card=@card where ID=3";
cmd.Parameters.AddWithValue("@card", "13000000000000");
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
conn.Dispose();
Console.WriteLine("修改成功!");
Console.ReadKey();
例4:向表中插入数据
string s = "data source=计算机名称;initial catalog=数据库名称;integrated security=true";
SqlConnection conn = new SqlConnection(s);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into T_User(UserName,Password,RealName,Mobile,Address) values(@username,@password,@realname,@mobile,@address)";
cmd.Parameters.AddWithValue("@username", "xingxing");
cmd.Parameters.AddWithValue("@password", "77777");
cmd.Parameters.AddWithValue("@realname", "星星");
cmd.Parameters.AddWithValue("@mobile", 1300000000);
cmd.Parameters.AddWithValue("@address", "河北省北京市");
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
conn.Dispose();
Console.WriteLine("成功插入一行");
Console.ReadKey();
(1)本地计算机连接;
复制代码 代码如下:
string s = "Data Source=计算机名称;initial Catalog=数据库名称;integrated Security=True";
(2)windows身份验证方式连接;
复制代码 代码如下:
string cc="Data Source = 计算机名称; Initial Catalog = 数据库名称; User ID = sa; Password = 你的密码";
二、在Visual Studio中使用:
例1:查询数据库中的数据并且显示出来
复制代码 代码如下:
string s = "Data Source=计算机名称;Initial Catalog=数据库名称;Integrated Security=True"; //此处使用本地计算机连接方式
SqlConnection conn = new SqlConnection(s); //创建连接
conn.Open(); //打开连接
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from T_User"; //使用命令
SqlDataAdapter adapter=new SqlDataAdapter(cmd);
DataTable dt=new DataTable();
adapter.Fill(dt);
conn.Dispose(); //释放所以资源
cmd.Dispose();
conn.Close(); //关闭连接
string realname="";
string username="";
string mobile="";
string address="";
for (int i=0;i
realname=dt.Rows[i][3].ToString();
username=dt.Rows[i][1].ToString();
mobile=dt.Rows[i][4].ToString();
address=dt.Rows[i][5].ToString();
Console.WriteLine("姓名为{0},用户名为{1},手机为{2},地址为{3}", realname, username, mobile, address);
}
Console.ReadKey();
例2:删除表中数据
复制代码 代码如下:
string cc="Data Source = 计算机名称; Initial Catalog = 数据库名称; User ID = sa; Password = 你的密码"; //使用windows身份验证
SqlConnection conn = new SqlConnection(s);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "delete from T_User where Id=5";
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
Console.WriteLine("删除成功");
Console.ReadKey();
例3:修改表中数据
复制代码 代码如下:
string s = "Data Source=计算机名称;initial Catalog=数据库名称;integrated Security=True";
SqlConnection conn = new SqlConnection(s);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "update T_User set Card=@card where ID=3";
cmd.Parameters.AddWithValue("@card", "13000000000000");
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
conn.Dispose();
Console.WriteLine("修改成功!");
Console.ReadKey();
例4:向表中插入数据
复制代码 代码如下:
string s = "data source=计算机名称;initial catalog=数据库名称;integrated security=true";
SqlConnection conn = new SqlConnection(s);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into T_User(UserName,Password,RealName,Mobile,Address) values(@username,@password,@realname,@mobile,@address)";
cmd.Parameters.AddWithValue("@username", "xingxing");
cmd.Parameters.AddWithValue("@password", "77777");
cmd.Parameters.AddWithValue("@realname", "星星");
cmd.Parameters.AddWithValue("@mobile", 1300000000);
cmd.Parameters.AddWithValue("@address", "河北省北京市");
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
conn.Dispose();
Console.WriteLine("成功插入一行");
Console.ReadKey();
您可能感兴趣的文章:
相关内容
- GridView使用CommandField删除列实现删除时提示确认框_实用技巧_
- DataGrid中实现超链接的3种方法_实用技巧_
- .Net下二进制形式的文件(图片)的存储与读取详细解析_实用技巧_
- DataSet.Tables[].Rows[][]的用法详细解析_实用技巧_
- 如何在asp.net中使用FreeTextBox控件_实用技巧_
- 将FreeTextBox做成控件添加到工具箱中的具体操作方法_实用技巧_
- 关于Asp.net页面Page_Load被执行两次的问题分享_实用技巧_
- ASP.NET MVC 从IHttp到页面输出的实例代码_实用技巧_
- 在子页中隐藏模板页中的div示例代码_实用技巧_
- ASP.NET中相对路径的使用总结_实用技巧_
