本文介绍了如何用C#中的参数编写这段代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我在c#中的更新代码:
this is my update code in c# :
private void BtnٍEdit_Click(object sender, EventArgs e)
{
string sqlcon = "Data Source=desktop-nee5996\\shadow;Initial Catalog=Filelib;Integrated Security=True";
SqlConnection cn = new SqlConnection(sqlcon);
try
{
if (TxtName.Text == "")
{
MessageBox.Show("Please Inset Name.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
cn.Open();
SqlStr = "update Filelib set ";
SqlStr = SqlStr + " Name='" + TxtName.Text.Trim() + "',";
SqlStr = SqlStr + " Year='" + TxtYear.Text.Trim() + "',";
SqlStr = SqlStr + " Director='" + TxtDirector.Text.Trim() + "',";
SqlStr = SqlStr + " Quality='" + TxtQuality.Text.Trim() + "',";
SqlStr = SqlStr + " Size='" + TxtSize.Text.Trim() + "'";
SqlStr = SqlStr + " where Id_Files='" + TxtId_Files.Text + "'";
sqlcmd = new SqlCommand(SqlStr, cn);
sqlcmd.ExecuteNonQuery();
MessageBox.Show("Edited succesefully.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
Filelibindex FrmTxtId_Files = new Filelibindex();
this.Hide();
FrmTxtId_Files.ShowDialog();
}
}
finally
{
cn.Close();
}
}
这是我的填充网格视图代码:
and this is my code for fill grid view :
public void fillGrid()
{
String sqlcon = "Data Source=desktop-nee5996\\shadow;Initial Catalog=Filelib;Integrated Security=True";
SqlConnection cn = new SqlConnection(sqlcon);
try
{
cn.Open();
SqlStr = "Select IdAttachBord,";
SqlStr = SqlStr + " FkIdFiles,";
SqlStr = SqlStr + " FilePath,";
SqlStr = SqlStr + " FileData,";
SqlStr = SqlStr + " FileName,";
SqlStr = SqlStr + " SafeFileName";
SqlStr = SqlStr + " from FilelibF";
SqlStr = SqlStr + " where FkIdFiles ='" + IdFileStudent.Text + "'";
SqlDataAdapter ADAP = new SqlDataAdapter(SqlStr, cn);
pagingAdapter = new SqlDataAdapter(SqlStr, cn);
pagingDS = new DataSet();
DS = new DataSet();
ADAP.Fill(DS, "FilelibF");
pagingAdapter.Fill(pagingDS, scrollVal, 7, "paging");
GridFile.DataSource = DS.Tables["FilelibF"];
GridFile.DataSource = pagingDS;
GridFile.DataMember = "paging";
GridFile.Columns[4].HeaderText = "File Name";
GridFile.Columns[0].Visible = false;
GridFile.Columns[1].Visible = false;
GridFile.Columns[2].Visible = false;
GridFile.Columns[3].Visible = false;
GridFile.Columns[5].Visible = false;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
i认为此代码有一些错误请告诉我如何修复错误以及如何用C#中的参数编写这两个代码?谢谢你。
i think this code have some mistakes please tell me how to fix mistakes and how to write these two codes with parameters in C# ? thank you.
推荐答案
SqlStr = "UPDATE Filelib SET Name='" + TxtName.Text.Trim() + "' WHERE Id_Files='" + TxtId_Files.Text + "'";
sqlcmd = new SqlCommand(SqlStr, cn);
sqlcmd.ExecuteNonQuery();
然后将其转换为:
Then just convert it to:
SqlStr = "UPDATE Filelib SET Name=@NM WHERE Id_Files=@ID";
sqlcmd = new SqlCommand(SqlStr, cn);
sqlcmd.Parameters.AddWithValue("@NM", TxtName.Text.Trim());
sqlcmd.Parameters.AddWithValue("@ID", TxtId_Files);
sqlcmd.ExecuteNonQuery();
按照你的整个代码的模式,你会没事的。
Follow that pattern for your whole code, and you'll be fine.
这篇关于如何用C#中的参数编写这段代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!