for (int i = 1; i <= daysCount; i++)
{
conn.Open();
string sQuery = "INSERT INTO TripSheet VALUES('" + lblDate1.Text + "','" + txtFuel1.Text+ "','" + txtRate1.Text + "')";
SqlCommand cmd = new SqlCommand(sQuery, conn);
cmd.ExecuteNonQuery();
conn.Close();
}
这里我要用count
月份,我必须使用循环插入月份数据。
lblDate1.Text, txtFuel1.Text & txtRate.Text
我想像
一样传递此值lblDate[i].Text, txtFuel[i].Text & txtRate[i].Text (Is it possible?)
答案 0 :(得分:0)
通常,我希望这些值在某种形式的对象集合中,这些对象已从用户界面中清除。例如,作为给定类型的List或and Array。我通常还会看到按数据类型插入SQL数据,例如,日期用datetime
,名称用VARCHAR(30)
等等。现在鉴于我对此没有可见性,因此我在这里使用了不太想要的AddWithValue
。我打的很快,所以可能是错误的。将其放在一组适当的类和文件中。
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Data.SqlClient;
namespace Rates
{
public class FuelRateThing
{
public DateTime RateDate
{
get;
set;
}
public decimal FuelRate
{
get;
set;
}
public string FuelName
{
get;
set;
}
public FuelRateThing()
{
}
public FuelRateThing(DateTime date, decimal rate, string name)
{
RateDate = date;
FuelRate = rate;
FuelName = name;
}
}
public class Ratethings
{
public void dostuff()
{
CultureInfo provider = CultureInfo.InvariantCulture;
//Create a class to manage these objects perhaps, here is a simple list example.
// populate your list - normally parse and validation of UI values:
List<FuelRateThing> monthRatesList = new List<FuelRateThing>()
{new FuelRateThing{RateDate = DateTime.ParseExact("20180101", "yyyyMMdd", provider), FuelRate = 3.45m, FuelName = "kerosene"}, new FuelRateThing{RateDate = DateTime.ParseExact("20180102", "yyyyMMdd", provider), FuelRate = 3.49m, FuelName = "kerosene"}, new FuelRateThing{RateDate = DateTime.ParseExact("20180103", "yyyyMMdd", provider), FuelRate = 3.48m, FuelName = "kerosene"}, new FuelRateThing{RateDate = DateTime.ParseExact("20180104", "yyyyMMdd", provider), FuelRate = 3.65m, FuelName = "kerosene"}, new FuelRateThing{RateDate = DateTime.ParseExact("20180105", "yyyyMMdd", provider), FuelRate = 3.60m, FuelName = "kerosene"}, };
// add a couple more rows
monthRatesList.Add(new FuelRateThing(DateTime.ParseExact("20180106", "yyyyMMdd", provider), 3.64m, "kerosene"));
var newrate = new FuelRateThing{RateDate = DateTime.ParseExact("20180107", "yyyyMMdd", provider), FuelRate = 3.47m, FuelName = "kerosene"};
monthRatesList.Add(newrate);
}
//Now use the data access code to insert those: (probably in a method that accepts a list of `FuelRateThings`:
public class DataStore
{
public void SaveRateThings(List<FuelRateThing> rateList)
{
var _connectionSB = new SqlConnectionStringBuilder();
// do connections string stuff here
var _connectionString = _connectionSB.ToString();
using (SqlConnection connection = new SqlConnection(_connectionString))
{
String query = @"
INSERT INTO TripSheet (DateColumnName,FuelColumnName,RateColumnName)
VALUES (@DateColumnName,@FuelColumnName,@RateColumnName)";
using (SqlCommand command = new SqlCommand(query, connection))
{
connection.Open();
foreach (FuelRateThing rate in rateList)
{
command.Parameters.AddWithValue("@DateColumnName", rate.RateDate);
command.Parameters.AddWithValue("@FuelColumnName", rate.FuelName);
command.Parameters.AddWithValue("@RateColumnName", rate.FuelRate);
int result = command.ExecuteNonQuery();
}
}
}
}
}
}
}
答案 1 :(得分:0)
INSERT INTO tab (id) VALUES (1), (2), (3);
或
INSERT INTO tab(a,b,c)
SELECT * FROM
(
VALUES
(1,2,3),
(10,20,30),
(100,200,300)
) s(a,b,c);
c#
string temp="";
for (int i = 1; i <= daysCount; i++)
{
if(i!=1)
temp+=",";
temp+="('" + lblDate1.Text + "','" + txtFuel1.Text+ "','" + txtRate1.Text + "')";
}
conn.Open();
string sQuery = "INSERT INTO TripSheet VALUES "+temp;
SqlCommand cmd = new SqlCommand(sQuery, conn);
cmd.ExecuteNonQuery();
conn.Close();