You can use Bulk Copy. Using this, first bulk insert all the records of x in a new table and bulk insert all records of Y in a new table.
Now you can use Cross joins in between these two new tables..like below
Select X.TableXVal, Y.TableYVal from NewTableX X
Cross Join NewTableY Y
This query can be written in the Stored Procedure and stored procedure can be called like below
using (System.Data.SqlClient.SqlConnection con = new SqlConnection("YourConnection string")) {
con.Open();
SqlCommand cmd = new SqlCommand();
string expression = "Parameter value";
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Your Stored Procedure";
cmd.Parameters.Add("Your Parameter Name",
SqlDbType.VarChar).Value = expression;
cmd.Connection = con;
using (IDataReader dr = cmd.ExecuteReader())
{
if (dr.Read())
{
}
}
}
You can create the sample tables like below
create table NewTableX
(
ID int Primary Identity(1,1),
TableXVal int
)
create table NewTableY
(
ID int Primary Identity(1,1),
TableYVal int
)
In this way, you can skip inserting the records one by one. hope this will help you.
For more information about using BulkCopy. Below is the code.
private void CreateDataTableFromList()
{
//supose you have list for X like below
List<int> x = new List<int>();
x.Add(1);
x.Add(2);
x.Add(3);
x.Add(4);
//supose you have list for Y like below
List<int> y = new List<int>();
y.Add(1);
y.Add(2);
y.Add(3);
y.Add(4);
DataTable dt = new DataTable();
DataColumn dc;
DataRow dr;
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.Int32");
dc.ColumnName = "TableXVal";
dt.Columns.Add(dc);
dr = dt.NewRow();
dr["TableXVal"] = 1;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["TableXVal"] = 2;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["TableXVal"] = 3;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["TableXVal"] = 4;
dt.Rows.Add(dr);
SqlBulkCopy copy = new SqlBulkCopy("Your Connection String");
copy.DestinationTableName = "NewTableX";
copy.WriteToServer(dt);
dt = new DataTable();
dc = new DataColumn();
dc.DataType = System.Type.GetType("System.Int32");
dc.ColumnName = "TableYVal";
dt.Columns.Add(dc);
dr = dt.NewRow();
dr["TableYVal"] = 1;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["TableYVal"] = 2;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["TableYVal"] = 3;
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["TableYVal"] = 4;
dt.Rows.Add(dr);
copy = new SqlBulkCopy("Your Connection String");
copy.DestinationTableName = "NewTableY";
copy.WriteToServer(dt);
}
Step1 - Use CreateDataTableFromList function
Step 2 - Call stored procedure as mentioned above
Your stored procedure must have the select statement as mentioned above.