[英]How to insert data table values into database
I have a table filled with data which contains columns 我有一个表填充了包含列的数据
Name Module1Marks Module2Marks Module3Marks
I have to insert this data into database table with the columns 我必须使用列将此数据插入到数据库表中
Name ModuleId Marks
Here by checking column name i have to insert ModuleId into database along with marks and name 这里通过检查列名称,我必须将ModuleId与标记和名称一起插入数据库
eg. 例如。 if columnname is Module1Marks then i will take ModuleId=1 and value in column Module1Marks of table as marks to insert into database 如果columnname是Module1Marks那么我将把ModuleId = 1和表的Module1Marks中的值作为标记插入到数据库中
Please help me - how do I do it? 请帮帮我 - 我该怎么办?
int ModuleId = 1;
foreach (DataRow row in dt.Rows)
{
for(int i = 0; i < dt.Columns.Count; i++)
{
if (dt.Columns[i].ColumnName == "OSC")
{
string marks = row[i].ToString();
NewInsertFunction(row[1].ToString(), row[2].ToString(), int.Parse(marks) , 1);
}
if (dt.Columns[i].ColumnName == "OOP")
{
string marks = row[i].ToString();
NewInsertFunction(row[0].ToString(), row[1].ToString(), int.Parse(marks), 2);
}
}
protected void NewInsertFunction(string PRN, string Name, int Marks, int ModuleId) {
int CourseID = Convert.ToInt32( DropDownListCourse.SelectedItem.Value);
Convert.ToInt32(Request.Form["DropDownListCourse"].ToString());
int startIndex = 4;
int endIndex = PRN.Length - 4;
int centreID = Convert.ToInt32(PRN.Substring(startIndex, endIndex)); }
Kindly have a look on the below code. 请看下面的代码。
private void ExtractData()
{
int moduleId;
int marks;
string name;
foreach (DataRow row in custTable.Rows)
{
name = row["Name"].ToString();
moduleId = 1;
marks = int.Parse(row["Module1Marks"].ToString());
NewInsertFunction(name, moduleId, marks);
moduleId = 2;
marks = int.Parse(row["Module2Marks"].ToString());
NewInsertFunction(name, moduleId, marks);
moduleId = 3;
marks = int.Parse(row["Module3Marks"].ToString());
NewInsertFunction(name, moduleId, marks);
}
}
private void NewInsertFunction(string Name, int ModuleId, int Marks)
{
// Provide the Insert logic
}
create user defined Table DataType and Pass your table to store procedure. 创建用户定义的表DataType并将表传递给存储过程。 Below link guide you create user defined table type 在链接指南下方创建用户定义的表类型
https://www.c-sharpcorner.com/article/user-defined-table-types-and-table-valued-parameters/ https://www.c-sharpcorner.com/article/user-defined-table-types-and-table-valued-parameters/
https://www.c-sharpcorner.com/blogs/userdefined-table-type-and-table-valued-parameters-in-stored-procedure-to-reduce-the-code-size-in-code-behind-file https://www.c-sharpcorner.com/blogs/userdefined-table-type-and-table-valued-parameters-in-stored-procedure-to-reduce-the-code-size-in-code-behind-文件
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.