首页上一页 1 下一页尾页 1 条记录 1/1页
为什么不能循环写入数据库?
发表在VB答疑区
2009-03-10
是否精华
是
否
版块置顶:
是
否
我用vb2005做了个程序,求出所有可能的解,并且把这些解写到数据库中去,但如何也不能写入,一条记录也写不进去,现把程序附上,请高手指点,谢谢!
Dim objConnection As New SqlConnection("server=acer-c72693603f\hexiaohui;database=GongYiXuanZe;User ID=sa;Password=hexiaohui;") '在程序开始时就初始化对数据库的连接
Dim objDataAdapter As New SqlDataAdapter("Select * From SimpleRoute", objConnection) '在程序开始时就初始化
Dim objDataSet As DataSet
'Dim objDataView As DataView
Private Sub FillDataSetAndView()
objDataSet = New DataSet() '初始化一个DataSet对象的新实例
objDataAdapter.Fill(objDataSet, "SimpleRoute") '用数据填充该DataSet对象
'objDataView = New DataView(objDataSet.Tables("SimpleRoute")) '把DataSet对象设置到数据视图中去
End Sub
....
Dim objCommand As SqlCommand = New SqlCommand '定义SQL执行语句
....
objConnection.Open()
objCommand.Connection = objConnection
TotalYsxs = BeforeChuanKong.Diameter * BeforeChuanKong.Diameter / (4 * txtChengpinBiHou.Text.ToString * (txtChengPinDiameter.Text.ToString - txtChengpinBiHou.Text.ToString)) '这里求处轧制该管坯所需要的总的延伸率。
If TotalYsxs <= 20 Then '对延伸率进行判定,本函数适合总体延伸率20以下的工艺流程
lsbGongYiXuanZe.Items.Add("穿孔系数" & " " & "穿后外径" & " " & "穿后壁厚" & _
" " & "拉拔系数" & " " & "拉后外径" & " " & "拉后壁厚" & " " & "冷轧系数" & _
" " & "轧后外径" & " " & "轧后壁厚") '把合格工序的标题在ListBox控件中显示出来,便于下一步的优化选择。
For ChuanKongYsxs As Single = 1.0 To 4.0 Step 0.01 '穿孔的延伸系数下限为1,上限为4
jmj = BeforeChuanKong.Diameter * BeforeChuanKong.Diameter / (4 * ChuanKongYsxs) '求出在该延伸率下的穿孔后的截面积
For AfterChuanKongBiHou As Single = BeforeChuanKong.Diameter / 20 To (BeforeChuanKong.Diameter + 10) / 6 Step 0.1
AfterChuanKong.Diameter = AfterChuanKongBiHou + jmj / AfterChuanKongBiHou '这里求出穿孔后该壁厚对应的管坯外径。
If AfterChuanKong.Diameter >= BeforeChuanKong.Diameter And AfterChuanKong.Diameter <= BeforeChuanKong.Diameter + 10 Then '表示穿孔合格,穿孔后外径最多比穿孔前外径大10毫米。
AfterChuanKong.BiHou = AfterChuanKongBiHou '把变量AfterChuanKongBiHou赋值给AfterChuanKong.BiHou,使AfterChuanKong变量各值有用,便于最后输出的正确。
BeforeLaBa.Diameter = AfterChuanKong.Diameter
BeforeLaBa.BiHou = AfterChuanKong.BiHou
For LaBaYsxs As Single = 1.0 To 1.4 Step 0.01 '拉拔的延伸系数下限为1,上限为1.4
For AfterLabaDiameter As Single = 0 To 1000 Step 1 '这里拉拔后外径取0---1000,是因为拉拔后外径有可能变大,也有可能变小。
AfterLaBa.Diameter = AfterLabaDiameter
jmj = BeforeLaBa.BiHou * (BeforeLaBa.Diameter - BeforeLaBa.BiHou) / LaBaYsxs '求出拉拔后对应延伸率下的截面积
AfterLaBa.BiHou = (AfterLaBa.Diameter - Sqrt(AfterLaBa.Diameter * AfterLaBa.Diameter - 4 * jmj)) / 2 '这里求出拉拔后对应指定外径下的壁厚
If AfterLaBa.BiHou > 0 And Abs(BeforeLaBa.BiHou - AfterLaBa.BiHou) < 2 Then '表示拉拔合格,壁厚必须大于0,减壁量必须小于某一设定值(2mm),外径变化量这里没有考虑。
LengZhaYsxs = TotalYsxs / (ChuanKongYsxs * LaBaYsxs) '这里求出要求的冷轧延伸系数。
If LengZhaYsxs >= 1 And LengZhaYsxs <= 5 Then '如果冷轧延伸系数在允许范围内
BeforeLengZha.Diameter = AfterLaBa.Diameter
BeforeLengZha.BiHou = AfterLaBa.BiHou
jmj = BeforeLengZha.BiHou * (BeforeLengZha.Diameter - BeforeLengZha.BiHou) / LengZhaYsxs
AfterLengZha.BiHou = (LengZhaDiameter - Sqrt(LengZhaDiameter * LengZhaDiameter - 4 * jmj)) / 2 '求出指定外径(成品管外径)下的壁厚
If AfterLengZha.BiHou - LengZhaBiHou > -0.001 * LengZhaBiHou And AfterLengZha.BiHou - LengZhaBiHou < 0.001 * LengZhaBiHou Then '如果成品管壁厚满足成品要求,表示该工序的选择合格
AfterLengZha.Diameter = LengZhaDiameter
If BeforeLengZha.BiHou / AfterLengZha.BiHou <= 2 And BeforeLengZha.Diameter / AfterLengZha.Diameter <= 2 And LengZhaYsxs > BeforeLengZha.BiHou / AfterLengZha.BiHou Then
lsbGongYiXuanZe.Items.Add(" " & String.Format("{0:n2}", ChuanKongYsxs) & " " & String.Format("{0:n0}", AfterChuanKong.Diameter) & " " & String.Format("{0:n2}", AfterChuanKong.BiHou) & _
" " & String.Format("{0:n2}", LaBaYsxs) & " " & String.Format("{0:n0}", AfterLaBa.Diameter) & " " & String.Format("{0:n2}", AfterLaBa.BiHou) & " " & String.Format("{0:n2}", LengZhaYsxs) & _
" " & String.Format("{0:n0}", AfterLengZha.Diameter) & " " & String.Format("{0:n2}", AfterLengZha.BiHou)) '把合格工序在ListBox控件中显示出来,便于下一步的优化选择。
objCommand.CommandText = "INSERT INTO SimpleRoute " & _
"(ChuanKongYsxs,ChuanKongDia,ChuanKongBiHou,LaBaYsxs,LaBaDia,LabaBiHou,LengZhaYsxs,LengZhaDia,LengZhaBiHou) " & _
"VALUES(@ChuanKongYsxs,@ChuanKongDia,@ChuanKongBiHou,@LaBaYsxs,@LaBaDia,@LabaBiHou,@LengZhaYsxs,@LengZhaDia,@LengZhaBiHou)"
objCommand.Parameters.AddWithValue("@ChuanKongYsxs", String.Format("{0:n2}", ChuanKongYsxs)).DbType = DbType.Single
objCommand.Parameters.AddWithValue("@ChuanKongDia", String.Format("{0:n0}", AfterChuanKong.Diameter)).DbType = DbType.Int16
objCommand.Parameters.AddWithValue("@ChuanKongBiHou", String.Format("{0:n2}", AfterChuanKong.BiHou)).DbType = DbType.Single
objCommand.Parameters.AddWithValue("@LaBaYsxs", String.Format("{0:n2}", LaBaYsxs)).DbType = DbType.Single
objCommand.Parameters.AddWithValue("@LaBaDia", String.Format("{0:n0}", AfterLaBa.Diameter)).DbType = DbType.Int16
objCommand.Parameters.AddWithValue("@LabaBiHou", String.Format("{0:n2}", AfterLaBa.BiHou)).DbType = DbType.Single
objCommand.Parameters.AddWithValue("@LengZhaYsxs", String.Format("{0:n2}", LengZhaYsxs)).DbType = DbType.Single
objCommand.Parameters.AddWithValue("@LengZhaDia", String.Format("{0:n0}", AfterLengZha.Diameter)).DbType = DbType.Int16
objCommand.Parameters.AddWithValue("@LengZhaBiHou", String.Format("{0:n2}", AfterLengZha.BiHou)).DbType = DbType.Single
objCommand.CommandType = CommandType.Text
Try
objCommand.ExecuteNonQuery()
Catch SqlExceptionErr As SqlException
MessageBox.Show(SqlExceptionErr.Message)
End Try
If Not cmbChuanKong.Items.Contains(String.Format("{0:n2}", ChuanKongYsxs)) Then '如果该穿孔延伸系数不存在数组中
cmbChuanKong.Items.Add(String.Format("{0:n2}", ChuanKongYsxs)) '把穿孔延伸系数传递到combox中去
End If
....
老是提示我变量名重复,变量必须唯一
请问该如何修改呢?
Dim objConnection As New SqlConnection("server=acer-c72693603f\hexiaohui;database=GongYiXuanZe;User ID=sa;Password=hexiaohui;") '在程序开始时就初始化对数据库的连接
Dim objDataAdapter As New SqlDataAdapter("Select * From SimpleRoute", objConnection) '在程序开始时就初始化
Dim objDataSet As DataSet
'Dim objDataView As DataView
Private Sub FillDataSetAndView()
objDataSet = New DataSet() '初始化一个DataSet对象的新实例
objDataAdapter.Fill(objDataSet, "SimpleRoute") '用数据填充该DataSet对象
'objDataView = New DataView(objDataSet.Tables("SimpleRoute")) '把DataSet对象设置到数据视图中去
End Sub
....
Dim objCommand As SqlCommand = New SqlCommand '定义SQL执行语句
....
objConnection.Open()
objCommand.Connection = objConnection
TotalYsxs = BeforeChuanKong.Diameter * BeforeChuanKong.Diameter / (4 * txtChengpinBiHou.Text.ToString * (txtChengPinDiameter.Text.ToString - txtChengpinBiHou.Text.ToString)) '这里求处轧制该管坯所需要的总的延伸率。
If TotalYsxs <= 20 Then '对延伸率进行判定,本函数适合总体延伸率20以下的工艺流程
lsbGongYiXuanZe.Items.Add("穿孔系数" & " " & "穿后外径" & " " & "穿后壁厚" & _
" " & "拉拔系数" & " " & "拉后外径" & " " & "拉后壁厚" & " " & "冷轧系数" & _
" " & "轧后外径" & " " & "轧后壁厚") '把合格工序的标题在ListBox控件中显示出来,便于下一步的优化选择。
For ChuanKongYsxs As Single = 1.0 To 4.0 Step 0.01 '穿孔的延伸系数下限为1,上限为4
jmj = BeforeChuanKong.Diameter * BeforeChuanKong.Diameter / (4 * ChuanKongYsxs) '求出在该延伸率下的穿孔后的截面积
For AfterChuanKongBiHou As Single = BeforeChuanKong.Diameter / 20 To (BeforeChuanKong.Diameter + 10) / 6 Step 0.1
AfterChuanKong.Diameter = AfterChuanKongBiHou + jmj / AfterChuanKongBiHou '这里求出穿孔后该壁厚对应的管坯外径。
If AfterChuanKong.Diameter >= BeforeChuanKong.Diameter And AfterChuanKong.Diameter <= BeforeChuanKong.Diameter + 10 Then '表示穿孔合格,穿孔后外径最多比穿孔前外径大10毫米。
AfterChuanKong.BiHou = AfterChuanKongBiHou '把变量AfterChuanKongBiHou赋值给AfterChuanKong.BiHou,使AfterChuanKong变量各值有用,便于最后输出的正确。
BeforeLaBa.Diameter = AfterChuanKong.Diameter
BeforeLaBa.BiHou = AfterChuanKong.BiHou
For LaBaYsxs As Single = 1.0 To 1.4 Step 0.01 '拉拔的延伸系数下限为1,上限为1.4
For AfterLabaDiameter As Single = 0 To 1000 Step 1 '这里拉拔后外径取0---1000,是因为拉拔后外径有可能变大,也有可能变小。
AfterLaBa.Diameter = AfterLabaDiameter
jmj = BeforeLaBa.BiHou * (BeforeLaBa.Diameter - BeforeLaBa.BiHou) / LaBaYsxs '求出拉拔后对应延伸率下的截面积
AfterLaBa.BiHou = (AfterLaBa.Diameter - Sqrt(AfterLaBa.Diameter * AfterLaBa.Diameter - 4 * jmj)) / 2 '这里求出拉拔后对应指定外径下的壁厚
If AfterLaBa.BiHou > 0 And Abs(BeforeLaBa.BiHou - AfterLaBa.BiHou) < 2 Then '表示拉拔合格,壁厚必须大于0,减壁量必须小于某一设定值(2mm),外径变化量这里没有考虑。
LengZhaYsxs = TotalYsxs / (ChuanKongYsxs * LaBaYsxs) '这里求出要求的冷轧延伸系数。
If LengZhaYsxs >= 1 And LengZhaYsxs <= 5 Then '如果冷轧延伸系数在允许范围内
BeforeLengZha.Diameter = AfterLaBa.Diameter
BeforeLengZha.BiHou = AfterLaBa.BiHou
jmj = BeforeLengZha.BiHou * (BeforeLengZha.Diameter - BeforeLengZha.BiHou) / LengZhaYsxs
AfterLengZha.BiHou = (LengZhaDiameter - Sqrt(LengZhaDiameter * LengZhaDiameter - 4 * jmj)) / 2 '求出指定外径(成品管外径)下的壁厚
If AfterLengZha.BiHou - LengZhaBiHou > -0.001 * LengZhaBiHou And AfterLengZha.BiHou - LengZhaBiHou < 0.001 * LengZhaBiHou Then '如果成品管壁厚满足成品要求,表示该工序的选择合格
AfterLengZha.Diameter = LengZhaDiameter
If BeforeLengZha.BiHou / AfterLengZha.BiHou <= 2 And BeforeLengZha.Diameter / AfterLengZha.Diameter <= 2 And LengZhaYsxs > BeforeLengZha.BiHou / AfterLengZha.BiHou Then
lsbGongYiXuanZe.Items.Add(" " & String.Format("{0:n2}", ChuanKongYsxs) & " " & String.Format("{0:n0}", AfterChuanKong.Diameter) & " " & String.Format("{0:n2}", AfterChuanKong.BiHou) & _
" " & String.Format("{0:n2}", LaBaYsxs) & " " & String.Format("{0:n0}", AfterLaBa.Diameter) & " " & String.Format("{0:n2}", AfterLaBa.BiHou) & " " & String.Format("{0:n2}", LengZhaYsxs) & _
" " & String.Format("{0:n0}", AfterLengZha.Diameter) & " " & String.Format("{0:n2}", AfterLengZha.BiHou)) '把合格工序在ListBox控件中显示出来,便于下一步的优化选择。
objCommand.CommandText = "INSERT INTO SimpleRoute " & _
"(ChuanKongYsxs,ChuanKongDia,ChuanKongBiHou,LaBaYsxs,LaBaDia,LabaBiHou,LengZhaYsxs,LengZhaDia,LengZhaBiHou) " & _
"VALUES(@ChuanKongYsxs,@ChuanKongDia,@ChuanKongBiHou,@LaBaYsxs,@LaBaDia,@LabaBiHou,@LengZhaYsxs,@LengZhaDia,@LengZhaBiHou)"
objCommand.Parameters.AddWithValue("@ChuanKongYsxs", String.Format("{0:n2}", ChuanKongYsxs)).DbType = DbType.Single
objCommand.Parameters.AddWithValue("@ChuanKongDia", String.Format("{0:n0}", AfterChuanKong.Diameter)).DbType = DbType.Int16
objCommand.Parameters.AddWithValue("@ChuanKongBiHou", String.Format("{0:n2}", AfterChuanKong.BiHou)).DbType = DbType.Single
objCommand.Parameters.AddWithValue("@LaBaYsxs", String.Format("{0:n2}", LaBaYsxs)).DbType = DbType.Single
objCommand.Parameters.AddWithValue("@LaBaDia", String.Format("{0:n0}", AfterLaBa.Diameter)).DbType = DbType.Int16
objCommand.Parameters.AddWithValue("@LabaBiHou", String.Format("{0:n2}", AfterLaBa.BiHou)).DbType = DbType.Single
objCommand.Parameters.AddWithValue("@LengZhaYsxs", String.Format("{0:n2}", LengZhaYsxs)).DbType = DbType.Single
objCommand.Parameters.AddWithValue("@LengZhaDia", String.Format("{0:n0}", AfterLengZha.Diameter)).DbType = DbType.Int16
objCommand.Parameters.AddWithValue("@LengZhaBiHou", String.Format("{0:n2}", AfterLengZha.BiHou)).DbType = DbType.Single
objCommand.CommandType = CommandType.Text
Try
objCommand.ExecuteNonQuery()
Catch SqlExceptionErr As SqlException
MessageBox.Show(SqlExceptionErr.Message)
End Try
If Not cmbChuanKong.Items.Contains(String.Format("{0:n2}", ChuanKongYsxs)) Then '如果该穿孔延伸系数不存在数组中
cmbChuanKong.Items.Add(String.Format("{0:n2}", ChuanKongYsxs)) '把穿孔延伸系数传递到combox中去
End If
....
老是提示我变量名重复,变量必须唯一
请问该如何修改呢?