YAKOVM
YAKOVM

Reputation: 10153

Fill data in SQL Server table

I am new to SQL and I am experimenting.

I have created a SQL Server Project in VS2013 and generated several tables using the GUI (using the auto-generated CREATE TABLE command).

Now I want to implement in C# a stored procedure to populate the tables with data statically before the deployment.

Could someone give me an example/link about how to do that?

Upvotes: 1

Views: 1068

Answers (1)

Sev09
Sev09

Reputation: 883

Here's an example of the syntax you can use to use SQL together with C#:

string stmt = "INSERT INTO dbo.Test(id, name) VALUES(@ID, @Name)";

SqlCommand cmd = new SqlCommand(stmt, _connection);
cmd.Parameters.Add("@ID", SqlDbType.Int);
cmd.Parameters.Add("@Name", SqlDbType.VarChar, 100);

for (int i = 0; i < 10000; i++)
{
    cmd.Parameters["@ID"].Value = i;
    cmd.Parameters["@Name"].Value = i.ToString();

    cmd.ExecuteNonQuery();
}

Source: C# SQL insert command

Upvotes: 1

Related Questions