当前位置:网站首页>donet framework4.X==windows窗体应用新建项目,通过System.Data.SqlClient连接sqlserver进行查询
donet framework4.X==windows窗体应用新建项目,通过System.Data.SqlClient连接sqlserver进行查询
2022-07-17 03:08:00 【一个java开发】





双击login按钮,注册事件,自动跳转到相关代码,程序运行的时候点击按钮就会自动执行对应代码

连接sqlserver数据库,查询数据
不使用占位符
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;
namespace WindowsFormsApp1sqlserver
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//获取到刚才可视化界面画的图形中的内容,根据属性中设置名称来获取
string _id = this.account.Text.Trim();
string _name = this.password.Text;
//从sqlserver里查询数据
SqlConnection conn= new SqlConnection("server=你的数据库IP地址;uid=sa;pwd=qwe20211114.;database=sqlserver2017_db");
conn.Open();
//sql server不能用user做表名
string sql = $"select id,name from usertable where id='{_id}' and name='{_name}'";
SqlCommand command= new SqlCommand(sql, conn);
SqlDataReader reader= command.ExecuteReader();
reader.Read();
string id= reader["name"].ToString();
MessageBox.Show(id);
conn.Close();
}
}
}
使用占位符防止sql注入
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;
namespace WindowsFormsApp1sqlserver
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string _id = this.account.Text.Trim();
string _name = this.password.Text;
SqlConnection conn= new SqlConnection("server=你的IP地址;uid=sa;pwd=qwe20211114.;database=sqlserver2017_db");
conn.Open();
//sql server不能用user做表名
string sql = $"select id,name from usertable where [email protected] and [email protected]";
SqlParameter[] parms = {
new SqlParameter("@userid",_id),
new SqlParameter("@username",_name)
};
SqlCommand command= new SqlCommand(sql, conn);
command.Parameters.AddRange(parms);
SqlDataReader reader= command.ExecuteReader();
reader.Read();
string id= reader["name"].ToString();
MessageBox.Show(id);
conn.Close();
}
}
}

边栏推荐
- A Tutorial on Learned Multi-dimensional Indexes
- Tutorial: Adaptive Replication and Partitioning in Data Systems
- (21) blender source code analysis: click the mouse to add the message to the queue
- 劍指 Offer 59 - II. 隊列的最大值
- Container adapter - stack, queue, priority queue
- Acwing: Game 60 of the week
- Nature Communications
- Artifact website directories are all websites that are just needed and easy to use
- Modify jar package content
- Enterprises love hybrid app development, and applet container technology can improve efficiency by 100%
猜你喜欢

Application of MATLAB in linear algebra

第2章——创建与维护MySQL数据库

Hcip day 8 notes

Styleflow concise reading: use continuous flow to complete attribute editing

The software supply chain security risk that makes enterprise digitalization and it executives bear the brunt of the cauldron points to the north

Efficientnet series (1): efficientnetv2 network details

Swagger

A Tutorial on Learned Multi-dimensional Indexes

Burpsuite2022.1 detailed installation steps include certificate installation
What happens when MySQL tables change from compressed tables to ordinary tables?
随机推荐
v-for 中 key 的作用
Container adapter - stack, queue, priority queue
Pinhole minimally invasive gingival surgery (pinhole gum rejuvenation)
[Paper Abstract] screenshots of methods for recording abstracts of interest and papers in special fields.
AcWing:第60场周赛
Acwing: Game 60 of the week
【LeetCode】745. Prefix and suffix search
Sparkcore core design: RDD, 220716,
Multivariate statistical analysis principal component analysis - 01
Common table expression CTE in Clickhouse
第2章——创建与维护MySQL数据库
【LeetCode】558. Intersection of quadtree
Chapter 4 user data analysis
厲害,竟然把VSCode玩成了IDEA的效果,有點哇塞
Get to know esp8266 (II) -- build a network server to realize remote control
Tutorial: Adaptive Replication and Partitioning in Data Systems
TS的使用案例——贪吃蛇
[try to hack] NTLM authentication process
Vision Transformer(1):Self-attention Multi-head Self-attention
Hcip seventh day notes