1

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?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
YAKOVM
  • 9,805
  • 31
  • 116
  • 217
  • [ADO.NET](http://msdn.microsoft.com/en-us/library/e80y5yhx(v=vs.110).aspx) and/or [The Entity Framework](http://msdn.microsoft.com/en-us/data/ef.aspx) will get you started. – Brian Dec 30 '13 at 20:28

1 Answers1

1

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

Community
  • 1
  • 1
Sev09
  • 883
  • 2
  • 12
  • 27
  • What is _connection?(I want to statically populate the table before the deployment) – YAKOVM Dec 30 '13 at 20:40
  • Sorry, "_connection" is a previously declared SQL connection. For example: Using _connection As New SqlConnection(connectionString) So the syntax is as follows: SqlCommand [alias name] = new SqlCommand([SQL statement], [sql connection]) – Sev09 Dec 30 '13 at 20:49
  • The tables are not on the server.I want to populate them and only after that deploy to the server.So I don`t think i need a conection .... – YAKOVM Dec 30 '13 at 20:51
  • 1
    @Yakov, even if you have tables created LOCALLY in SQL Server, you can have a connection string, such as: "Initial Catalog=TestDatabase;Data Source=(local);" – Sev09 Dec 30 '13 at 20:55