请使用NHibernate中的存储过程为我提供在表中插入值的示例代码。
此致 Jcreddy
答案 0 :(得分:1)
Here is a nice example使用NHibenate证明插入物。
答案 1 :(得分:0)
检查此代码是否符合您的要求。首先,您需要在db。
中创建存储过程ALTER程序[dbo]。[ninsert]
(@ header AS int,@ subtype AS varchar(50),@ title AS varchar(50), @description AS
VARCHAR(50))插入 dbo.news(nid,title,subtitle,description)值
(@头,@亚型,@标题,@描述)
然后您必须通过程序中的数据层访问此代码....
公共类datacheck { public SqlConnection con = new SqlConnection(@“Data Source = xxx \ SQLEXPRESS; Initia Catalog = eyepax; Integrated Security = True”);
public int SQLSP(string strSQL, int headerid, string subtype, string title,string descrip) // stored procedure { if (con.State.ToString() == "Closed") { con.Open(); } //con.Open(); SqlCommand sqlCmd = new SqlCommand(strSQL, con); sqlCmd.CommandType = CommandType.StoredProcedure; sqlCmd.Parameters.AddWithValue("@header", headerid); sqlCmd.Parameters.AddWithValue("@subtype", subtype); sqlCmd.Parameters.AddWithValue("@title", title); sqlCmd.Parameters.AddWithValue("@description", descrip); int results = sqlCmd.ExecuteNonQuery(); //sqlCmd.Dispose(); //con.Close(); return 0; } public DataSet executeSql() { DataSet ds = new DataSet(); if (con.State.ToString() == "Closed") { con.Open(); } string sqlcmd1 = "select * from news"; SqlCommand cmd = new SqlCommand(sqlcmd1, con); SqlDataAdapter sqlAda = new SqlDataAdapter(cmd); sqlAda.Fill(ds,"news"); return ds; }
public DataSet executeSql(sqlcmd)
{ DataSet ds = new DataSet(); if (con.State.ToString() == "Closed") { con.Open(); } SqlCommand cmd = new SqlCommand(sqlcmd, con); SqlDataAdapter sqlAda = new SqlDataAdapter(cmd); sqlAda.Fill(ds,"news"); return ds; }
用于插入,更新删除
public int SQLC(string strSQL)// for insert,update.delete // { // if(con.State.ToString()==“Closed”) // { // con.Open(); //}
// SqlCommand sqlCmd = new SqlCommand(strSQL,con);
// int result = sqlCmd.ExecuteNonQuery();
//返回结果; //}