登陆窗体如何连接数据库

登陆窗体如何连接数据库,第1张

//delphi的用ado连接

//adoquery-数据

adoquery1close; //关闭连接

adoquery1ConnectionString:='Provider=MicrosoftJetOLEDB40;Data Source='+数据库路径+';Persist Security Info=False';//数据连接字符串

with adoquery1 do

begin

close; //关闭

sqlclear;//清空sql语句

//查询密码帐户sql语句

sqltext:='select from login where name=:name and passwor=:pwd';

ParametersParamByName('name')Value:=tbnametext; //给参数name赋值

ParametersParamByName('pwd')Value:=tbmimatext; //给参数pwd赋值

open;//打开

//当查询出的条件大于0,就说明有这个账号和密码

if recordcount >0 then

begin //当存在该账号和密码就执行。。。。

showmessage('登陆成功');

//显示主窗体

end

else //不存在就执行以下代码

begin

showmessage('用户名或密码错误,请重新输入');

tbnameclear; //清空

tbmimaclear; //清空

tbnamesetfocus;//设置焦点

end;

end;

这个是个例子,供你参考,买些书看看,或着去网上下载些源码看看别人怎么写的

这个是学生成绩查询的代码

using System;

using SystemCollectionsGeneric;

using SystemComponentModel;

using SystemData;

using SystemDrawing;

using SystemText;

using SystemWindowsForms;

using SystemDataSqlClient;

namespace 学生管理系统

{

public partial class 成绩查询 : Form

{

private SqlConnection sqlconnection1;

private SqlCommand sqlcommand1;

private SqlDataAdapter sqldataadapter1;

DataSet dataset1;

private string conn = "workstation id=localhost;Integrated Security=SSPI;database=StuMagSys";

string str;

public 成绩查询()

{

InitializeComponent();

}

private void 成绩查询_Load(object sender, EventArgs e)

{

sqlconnection1 = new SqlConnection(conn);

str = "select coursecourse_name,student_coursestudent_id,student_coursescore,student_coursecredit,student_coursesc_semester,student_courseschool_year from student_course inner join course on student_coursecourse_id=coursecourse_id";

sqldataadapter1 = new SqlDataAdapter(str, sqlconnection1);

dataset1 = new DataSet();

dataset1Clear();

sqldataadapter1Fill(dataset1, "course");

dataGridView1DataSource = dataset1Tables[0];

}

private void btnquerybyname_Click(object sender, EventArgs e)

{

sqlconnection1 = new SqlConnection(conn);

str = str = "select coursecourse_name,student_coursestudent_id,student_coursescore,student_coursecredit,student_coursesc_semester,student_courseschool_year from student_course inner join course on student_coursecourse_id=coursecourse_id";

str = str + " and coursecourse_name like '%" + txtstunameTextTrim() + "%' ";

sqldataadapter1 = new SqlDataAdapter(str, sqlconnection1);

dataset1 = new DataSet();

dataset1Clear();

sqldataadapter1Fill(dataset1, "course");

dataGridView1DataSource = dataset1Tables[0];

}

private void bndelected_Click(object sender, EventArgs e)

{

if (MessageBoxShow("您确认要删除该记录吗?", "确认", MessageBoxButtonsYesNoCancel) == DialogResultYes)

{

string stu_id = thisdataGridView1CurrentRowCells[1]ValueToString();//获取选中行的学生ID号

str = "delete from student_course where student_id=" + stu_id + " ";

sqlconnection1 = new SqlConnection(conn);//创建连接

sqlcommand1 = new SqlCommand(str, sqlconnection1);//创建命令

sqlconnection1Open();//打开连接

int sum = sqlcommand1ExecuteNonQuery();

if (sum > 0) MessageBoxShow("删除成功!", "提示");

sqlconnection1Close();//关闭连接

//调用查询按钮的代码,刷新窗体上的datagridview里的数据

thisbtnquerybyname_Click(sender, e);

}

}

private void bnexit_Click(object sender, EventArgs e)

{

thisClose();

}

private void bnexit1_Click(object sender, EventArgs e)

{

thisClose();

}

private void btnedit_Click(object sender, EventArgs e)

{

if (dataGridView1SelectedCellsCount > 0)

{

xkbianj fn = new xkbianj(dataGridView1);

fnShow();

}

else MessageBoxShow(" 请选择一行数据");

}

private void groupBox1_Enter(object sender, EventArgs e)

{

}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

{

}

}

}

//连接你的数据库

string connstr = "server=;uid=sa;pwd=sa;database=sc";

using (SqlConnection conn = new SqlConnection(connstr))

{

connOpen();

DataSet ds = new DataSet();

SqlDataAdapter da = new SqlDataAdapter(sqlsel, conn);

daFill(ds, "coust");

DataTable dt = dsTables["coust"];

connClose();

return dt;

// TODO: 这行代码将数据加载到表“studentDataSetstudentinfo”中。您可以根据需要移动或移除它。

//thisstudentinfoTableAdapterFill(thisstudentDataSetstudentinfo);

//数据绑定

string sqlsel = "select from studentinfo ";

DataTable dt = bangding(sqlsel);

cmAmend = (CurrencyManager)BindingContext[dt];

thisdataGridView1DataSource = dt;

thistextBox1DataBindingsAdd("text", dt, "sno");

thistextBox2DataBindingsAdd("text", dt, "sname");

thistextBox3DataBindingsAdd("text", dt, "ssex");

thistextBox4DataBindingsAdd("text", dt, "sage");

thistextBox5DataBindingsAdd("text", dt, "sclass");

thistextBox6DataBindingsAdd("text", dt, "sdept");

thistextBox7DataBindingsAdd("text", dt, "saddress");

thistextBox8DataBindingsAdd("text", dt, "sphone");

thistextBox9DataBindingsAdd("text", dt, "sqq");

以上就是关于登陆窗体如何连接数据库全部的内容,包括:登陆窗体如何连接数据库、c#windows窗体编写学生管理系统、datagridview,与数据库连接的vs,windows窗体,引入的是sql server表格等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9752394.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-01
下一篇 2023-05-01

发表评论

登录后才能评论

评论列表(0条)

保存