在ASP.NET类中实现与SQL Server的通信 [英] Implement communication with SQL server in ASP.NET class
本文介绍了在ASP.NET类中实现与SQL Server的通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在asp.net类中为sql server插入更新删除代码 - 如何为sql server更改此代码
class 1
insert-update-delete code for sql server in asp.net class--How do I change this code for sql server
class 1
public class NoeAutoE
{
public UInt64 NoeAutoId { get; set; }
public String Nam { get; set; }
public String Tozihat { get; set; }
}
class 2
class 2
public class NoeAutoEx
{
public static void Insert(NoeAutoE nau)
{
String SQL = "INSERT INTO NoeAuto (NoeAutoId,Nam,Tozihat)VALUES(" + nau.NoeAutoId.ToString() + ",'"+nau.Nam+"','"+nau.Tozihat+"')";
DataBase db = new DataBase();
db.Execute(SQL);
}
public static void Update(NoeAutoE nau)
{
String SQL = "UPDATE NoeAuto SET Nam='" + nau.Nam + "',Tozihat='" + nau.Tozihat + "' WHERE NoeAutoId=" + nau.NoeAutoId.ToString();
DataBase db = new DataBase();
db.Execute(SQL);
}
public static void Delete(NoeAutoE nau)
{
String SQL = "DELETE FROM NoeAuto WHERE NoeAutoId=" + nau.NoeAutoId.ToString();
DataBase db = new DataBase();
db.Execute(SQL);
}
public static DataTable Fill()
{
String SQL = "SELECT * FROM NoeAuto Order By NoeAutoId DESC";
DataBase db = new DataBase();
DataTable dt = new DataTable();
dt = db.ExecuteSelect(SQL);
return dt;
}
public static UInt64 FindNextNoeAutoId()
{
String SQL = "SELECT MAX(NoeAutoId) FROM NoeAuto";
DataBase db = new DataBase();
DataTable dt = new DataTable();
dt = db.ExecuteSelect(SQL);
if (dt.Rows[0][0].ToString() == "")
{
return 1;
}
else
{
return Convert.ToUInt64(dt.Rows[0][0].ToString()) + 1;
}
}
public static NoeAutoE Fill(UInt64 NoeAutoId)
{
String SQL = "SELECT * FROM NoeAuto WHERE NoeAutoId=" +NoeAutoId.ToString() ;
DataBase db = new DataBase();
DataTable dt = new DataTable();
dt = db.ExecuteSelect(SQL);
NoeAutoE nau = new NoeAutoE();
nau.NoeAutoId = UInt64.Parse(dt.Rows[0]["NoeAutoId"].ToString());
nau.Nam = dt.Rows[0]["Nam"].ToString();
nau.Tozihat = dt.Rows[0]["Tozihat"].ToString();
return nau;
}
public static void FilldrpNoeAuto(ref DropDownList drpNoeAuto)
{
String SQL = "SELECT * FROM NoeAuto";
DataBase db = new DataBase();
DataTable dt = new DataTable();
dt = db.ExecuteSelect(SQL);
for (int i = 0; i < dt.Rows.Count; i++)
{
drpNoeAuto.Items.Add(dt.Rows[i]["Nam"].ToString());
}
}
}
如何为sql server更改此代码???
How do I change this code for sql server???
推荐答案
您好,
对于插入/更新/删除,您可以使用
Hi,
For Insert/Update/Delete you can use
public bool Insert(string sQry)
{
try
{
con = new SqlConnection(WebConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand(sQry, con);
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
return false;
}
finally
{
con.Close();
}
}
对于选择查询,您可以使用以下代码
And for select query you can use below code
public DataTable LoadDataTable(string sQry)
{
DataTable dt = new DataTable();
try
{
con = new SqlConnection(WebConfigurationManager.ConnectionStrings["connectionstring"].ConnectionString);
SqlDataAdapter sDap = new SqlDataAdapter(sQry, con);
sDap.Fill(dt);
return dt;
}
catch (Exception ex)
{
return null;
}
}
上面的代码很简单,只是为了用asp开始编码。净与sql,但在使用存储过程解雇的专业工作查询...
Above code is vary simple and just for start coding with asp.net & sql , but in professional work query fired using stored procedure...
这篇关于在ASP.NET类中实现与SQL Server的通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文