我有一个46列的Datagridview,包括Date,Int和String Type。我想将值插入另一个oracle表。
INNER JOIN GLDBFA.GLPDA PDA ON PCT.CTCO=PDA.DACO
LEFT OUTER JOIN GLDBFA.GLPBX PBX
ON CTCO=PBX.BXCO
AND CTPAGE=PBX.BXPAGE
AND PDA.DAYEAR=PBX.BXYEAR
AND PGL.GLMORS=PBX.BXMORS -- <<< "PGL" is unknown at this point
-- it is only introduced on the next line
-- and this condition is redundant anyway
-- so it can be safely removed
LEFT OUTER JOIN GLDBFA.GLPGL PGL
ON CTCO=PGL.GLCO
AND CTPAGE=PGL.GLPAGE
AND PDA.DAYEAR=PGL.GLYEAR
AND PGL.GLMORS=PBX.BXMORS
我不太明白我在传递参数时做错了什么。我是C#的新手,将数据类型和值传递给另一个oracle表的正确方法是什么?
答案 0 :(得分:1)
根据您使用的Oracle驱动程序,<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApplication1"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid >
<ProgressBar
Name="progressBar1"
Value="0"
Height="33"
Width="300"
Foreground="SkyBlue"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Margin="0,220,0,0"/>
<Grid Name="myGrid" Margin="0,100,0,0" >
<Ellipse
Name="ellipse"
Width="65"
Height="65"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Stroke="SkyBlue"
Fill="SkyBlue"
Margin="-300,0,0,0" />
<TextBlock
Text="{Binding ElementName=progressBar1, Path=Value}"
Foreground="White"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontSize="16"
Margin="-300,0,0,0" />
</Grid>
</Grid>
</Window>
public partial class MainWindow : Window
{
private Timer _timer;
private FrameworkElement _decorator;
public MainWindow()
{
InitializeComponent();
this.Loaded += OnLoaded;
}
private void OnLoaded(object sender, RoutedEventArgs routedEventArgs)
{
_timer = new Timer(500);
_timer.Elapsed += TimerOnElapsed;
_timer.Start();
}
private void TimerOnElapsed(object sender, ElapsedEventArgs elapsedEventArgs)
{
Dispatcher.Invoke(DispatcherPriority.Normal, new Action(() =>
{
progressBar1.Value += 4;
var grid = VisualTreeHelper.GetChild(progressBar1, 0) as Grid;
if (grid != null)
_decorator = grid.Children.OfType<FrameworkElement>().FirstOrDefault(x => x.Name == "PART_Indicator");
if (_decorator != null)
myGrid.RenderTransform = new TranslateTransform(_decorator.ActualWidth, myGrid.RenderTransform.Value.OffsetY);
}));
}
}
方法可能存在重载,该方法接受该值作为第二个参数,或者可能有.Add
方法明确地执行此操作。我认为对于ODP.net而言,你只需要.AddWithValue
。
无论哪种方式,这些都将从值的数据类型派生数据类型。
关于参数,在调用参数时需要省略冒号.Add
字符。我知道在SQL Server,Sybase,SQLite和其他人中包含它们(在这些情况下为:
),但在Oracle中,您在参数声明期间将它们排除在外。有趣的是,我认为PostgreSQL接受这两种方法。
因此,我认为这将有效:
@
如果没有,可以尝试使用类型或值进行显式转换:
呸:
private void writeRecord(DataGridViewRow datarow)
{
string sqlqry = "insert into Table (Date,Number) values(:Date,:Number)";
OracleCommand cmd = new OracleCommand(sqlqry, conn);
cmd.Parameters.Add(new OracleParameter("Date", datarow["DATE"]))
cmd.Parameters.Add(new OracleParameter("Number", datarow["Number"]));
// cmd.CommandText = sqlqry; -- not necessary, handled in constructor
cmd.ExecuteNonQuery();
}
更好:
cmd.Parameters.Add(new OracleParameter("Date", Convert.ToDateTime(datarow["DATE"])))
cmd.Parameters.Add(new OracleParameter("Number", Convert.ToDecimal(datarow["Number"])));
如果情况变得更糟,那么防弹(虽然冗长)的方法是:
cmd.Parameters.Add(new OracleParameter("DATE", OracleDbType.Date,
datarow["DATE"], ParameterDirection.Input);
cmd.Parameters.Add(new OracleParameter("Number", OracleDbType.Decimal,
datarow["Number"], ParameterDirection.Input));
作为脚注,如果您要插入多行,则最后一种方法实际上是首选,因为您创建一次参数并修改值并多次执行插入:
cmd.Parameters.Add(new OracleParameter("DATE", OracleDbType.Date));
cmd.Parameters.Add(new OracleParameter("Number", OracleDbType.Decimal));
cmd.Parameters[0].Value = datarow["DATE"];
cmd.Parameters[1].Value = datarow["Number"];
- 编辑 -
根据您的反馈和我提高的校对技巧,这就是我推荐的内容。我在那里添加了一个交易以获得良好的衡量标准:
cmd.Parameters.Add(new OracleParameter("DATE", OracleDbType.Date));
cmd.Parameters.Add(new OracleParameter("Number", OracleDbType.Decimal));
foreach (DataRow datarow in datarows)
{
cmd.Parameters[0].Value = datarow["DATE"];
cmd.Parameters[1].Value = datarow["Number"];
cmd.ExecuteNonQuery();
}
为了它的价值,你还需要一些异常处理。
答案 1 :(得分:0)
你可能想要使用:
cmd.Parameters.Add(new OracleParameter(DbType.Date, datarow["DATE"]))
cmd.Parameters.Add(new OracleParameter(DbType.Double, datarow["Number"]));
请参阅https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlparameter.dbtype(v=vs.110).aspx