1

Please provide me sample code for inserting values in a table using stored procedure in NHibernate.

Regards Jcreddy

2 Answers 2

1

Here is a nice example from Ayende demonstrating insert using NHibenate.

Sign up to request clarification or add additional context in comments.

Comments

0

Check if this code does what you want. First you need to create the stored procedure in db.

ALTER procedure [dbo].[ninsert]

(@header AS int,@subtype AS varchar(50),@title AS varchar(50), @description AS

VARCHAR(50)) as insert into dbo.news(nid,title,subtitle,description) values

(@header,@subtype,@title,@description)

then you must access this code via your data layer in the program....

public class 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;
}

for insert, update delete

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();
// return result; //}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.