| |

VerySource

 Forgot password?
 Register
Search
View: 561|Reply: 3

Ask how to execute the insert sqlserver stored procedure!

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-31 19:20:01
| Show all posts |Read mode
I am learning to use sqlserver stored procedure to insert a piece of data into the database, there are only two items in my data table,
ticketID (incremented), ticketName
Stored procedure I wrote:
CREATE PROCEDURE AddSpecialTicket @zw ticketName (200) AS
insert into SpecialTicket (ticketName) values ​​(@ticketName)

Please tell me how to use c # + asp.net to execute this stored procedure! I just started learning, read many books and write according to it is not enough (the book says using SqlDateAdapter), please list the steps in detail! Thank you.
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-3-11 10:15:01
| Show all posts
I heard that using stored procedures is a good programming practice and a more standardized programming method, but I feel that it is too cumbersome. It affects the work efficiency too much, and every time I manually write the stored procedure and then execute (the parameters must be set Come and set), it is really not as fast as writing sql and then executing it.
For example, I used sql to do the above:
string sql = "INSERT INTO SpecialTicket (ticketName) VALUES (" '"+ title.Text +"') ";
SqlCommand myCommand = new SqlCommand (sql, myConnection);
myConnection.Open ();
myCommand.ExecuteNonQuery ();
myConnection.Close ();
Reply

Use magic Report

2

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-3-11 21:30:01
| Show all posts
public DataSet ReturnDataSet (string proc, SqlParameter [] paramlist)
    {
        SqlConnection conn = new SqlConnection (ConfigurationManager.ConnectionStrings ["AdsysConnectionString"]. ConnectionString);
        conn.Open ();
        SqlCommand cmd = new SqlCommand ();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = proc;
        if (paramlist! = null&&paramlist.Length> 0)
        {
            foreach (SqlParameter param in paramlist)
            {
                cmd.Parameters.Add (param);
            }
        }
        SqlDataAdapter ada = new SqlDataAdapter (cmd);
        DataSet ds = new DataSet ();
        ada.Fill (ds);
        conn.Close ();
        return ds;

    }

Write a method in the entity class, it is OK to directly enter the parameter list and the stored procedure name,
Reply

Use magic Report

2

Threads

5

Posts

6.00

Credits

Newbie

Rank: 1

Credits
6.00

 China

Post time: 2020-3-12 06:30:01
| Show all posts
The above is returned, this is not! ~
 public void ExeuteNoQuery (string proc, SqlParameter [] paramlist)
    {
        SqlConnection conn = new SqlConnection (ConfigurationManager.ConnectionStrings ["AdsysConnectionString"]. ConnectionString);
        conn.Open ();
        SqlCommand cmd = new SqlCommand ();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = proc;

        if (paramlist! = null&&paramlist.Length> 0)
        {
            foreach (SqlParameter param in paramlist)
            {
                cmd.Parameters.Add (param);
            }
        }
        cmd.ExecuteNonQuery ();
        conn.Close ();

    }
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list