建立在C#中的SQL连接 [英] Creating a sql connection in c#
问题描述
我是新来的这个网站,也编程。我是通过销售点目前正在创建的库存系统。它采用模态和非模态形式。我的问题是寿,我的工作对更改密码
对话框,必须连接到数据库,以覆盖密码字段。我使用的数据库是Microsoft SQL Server管理工作室EX preSS。以下是我迄今为止必要的注释。请注意,设计的形式,我有这一定到数据库的组合框。我哪里做错了?
私人无效ChangePwdButton_Click(对象发件人,EventArgs的)
{
SqlConnection的sqlconn =新的SqlConnection();
sqlconn.ConnectionString = @数据源= \ SQLEX $ P $干燥综合征; AttachDbFilename = C:\用户\ Gerald-院长马丁\文档\ SQL Server Management Studio中前preSS \项目\ BodyMates.mdf;集成安全性=真;用户实例=真;
sqlconn.Open();
字符串OLDPWD = txtOldPwd.Text;
字符串NEWPWD = txtNewPwd.Text;
字符串confirmNewPwd = txtConfirmNewPwd.Text;
字符串sqlquery的=UPDATE [雇员] SET PWD = @ newpass,其中职工code = @ EMP code;
的SqlCommand CMD =新的SqlCommand(sqlquery的,sqlconn);
cmd.Parameters.AddWithValue(@ newpass,txtConfirmNewPwd.Text);
cmd.Parameters.AddWithValue(@ EMP code,comboEmp code.SelectedValue);
//cmd.Parameters.AddWithValue("@pwd,txtNewPwd.Text);
cmd.Connection = sqlconn;
cmd.ExecuteNonQuery();
SqlDataReader的博士= cmd.ExecuteReader();
而(dr.Read())
{
如果(txtOldPwd.Text ==博士[PWD]的ToString()及。及(txtNewPwd.Text == txtConfirmNewPwd.Text))
{
如果(comboEmp code.SelectedIndex == 0)
{
查询字符串=UPDATE [雇员] SET PWD ='+ txtConfirmNewPwd.Text +';
}
}
//如果(。(txtNewPwd.Text ==博士[NEWPWD]的ToString())及(txtConfirmNewPwd.Text ==(DR [confirmNewPwd]的ToString()))){}
}
//的MessageBox.show(密码更改成功!,更改密码,MessageBoxButtons.OK,MessageBoxIcon.Information);
}
您可以使用的 的ExecuteNonQuery
像 cmd.ExecuteNonQuery();
,返回 INT
值。使用像这样;
INT I = cmd.ExecuteNonQuery();
和也 的ExecuteReader()
是这样工作的;
SqlDataReader的读卡器= Command.ExecuteReader却();
而(reader.Read())
{
Console.WriteLine(的String.Format({0},读者[0]));
}
您可以读取返回数据的列。就像第一列阅读[0]
,第二列阅读器[1]
等
但在所有这些信息,如果你是新的节目,你可以找到很多的书建议,并在#1有趣的信息。检查这些物品;
- <一个href="http://stackoverflow.com/questions/1711/what-is-the-single-most-influential-book-every-programmer-should-read">What是最有影响力的书每个程序员都应该阅读?
- 什么是最好的C#的书?
- 最好的书一个新的数据库开发
I'm new to this site and also to programming. I am currently creating an inventory system via a point of sale. It uses modal and non-modal forms. My problem is tho, I'm working on the change password
dialog which has to be connected to the database in order to overwrite the password field. The database i used is microsoft sql server management studio express. Here is what I have so far with the necessary comments. Please note that on the 'design' form, I have a combobox which is bounded to the database. Where did I go wrong?
private void ChangePwdButton_Click(object sender, EventArgs e)
{
SqlConnection sqlconn = new SqlConnection();
sqlconn.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Gerald- dean Martin\Documents\SQL Server Management Studio Express\Projects\BodyMates.mdf;Integrated Security=True;User Instance=True";
sqlconn.Open();
string oldpwd = txtOldPwd.Text;
string newpwd = txtNewPwd.Text;
string confirmNewPwd = txtConfirmNewPwd.Text;
string sqlquery = "UPDATE [Employee] SET Pwd=@newpass where EmployeeCode=@empcode";
SqlCommand cmd = new SqlCommand(sqlquery, sqlconn);
cmd.Parameters.AddWithValue("@newpass", txtConfirmNewPwd.Text);
cmd.Parameters.AddWithValue("@empcode", comboEmpCode.SelectedValue);
//cmd.Parameters.AddWithValue("@pwd", txtNewPwd.Text);
cmd.Connection = sqlconn;
cmd.ExecuteNonQuery();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if(txtOldPwd.Text == dr["pwd"].ToString() && (txtNewPwd.Text == txtConfirmNewPwd.Text))
{
if (comboEmpCode.SelectedIndex == 0)
{
string query = "UPDATE [Employee] SET Pwd = '" + txtConfirmNewPwd.Text + "'";
}
}
// if ((txtNewPwd.Text == dr["newpwd"].ToString()) & (txtConfirmNewPwd.Text == (dr["confirmNewPwd"].ToString()))) { }
}
// MessageBox.Show("Password was changed Successfully!", "Password Change", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
You can use ExecuteNonQuery
like cmd.ExecuteNonQuery();
It returns int
value. Use it like this;
int i = cmd.ExecuteNonQuery();
And also ExecuteReader()
works like this;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
Console.WriteLine(String.Format("{0}", reader[0]));
}
You can read returning data's column. Like first column reader[0]
, second column reader[1]
etc.
But before all this information, if you are new to programming, you can find a lot of book proposal and useful informations on Stackoverflow. Check these articles;
- What is the single most influential book every programmer should read?
- What are the best C# books?
- Best Book for a new Database Developer
这篇关于建立在C#中的SQL连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!