vs2010往oracle 10g中插入数据

继承“vs2010连接数据库”文章的代码,修改地方使用红色字表示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Oracle.DataAccess.Client;

namespace dbtest2
{
    class Program
    {
        static void Main(string[] args)
        {
              string CONN_STR = "Data Source=(DESCRIPTION="
                + "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.128.152)(PORT=1521)))"
                + "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=LYNN)));"
                + "User Id=fangl;Password=fangl;";

              string queryString = "INSERT INTO auto_model(ID,NAME,DESCRIPTION) values (3,'车型3','这是车型3')";

              if (string.IsNullOrEmpty(CONN_STR))//指示指定的字符串是 null 还是 Empty 字符串。
              {
                  throw new Exception("数据库访问服务出现故障,请联系晨旭公司!");
              }
              else
              {
                  OracleConnection conn = null;
                  OracleCommand command = new OracleCommand(queryString);
                  Console.WriteLine("{0}", queryString);
 

                  try
                  {
                      conn = new OracleConnection(CONN_STR);
                      command.Connection = conn;
                      conn.Open();
                      Console.WriteLine("正在连接");
                      command.ExecuteNonQuery();//执行 SQL INSERT、DELETE、UPDATE 和 SET                     语句等命令。
                      }
                  catch (Exception e)
                  {
                      Console.WriteLine("{0}second exception caught",e);
                      Console.ReadLine();
                  }
                  Console.WriteLine("连接成功");
                  Console.ReadLine();
                  if (null != conn)
                  {
                      conn.Close();
                      conn.Dispose();
                  }
              }
        }
    }
}
另外,说明command的另外几个的属性:

ExecuteReader:执行返回行的命令。

ExecuteOracleNonQuery:针对 Connection 执行 SQL 语句并返回受影响的行数。

如:

public int ExecuteOracleNonQuery (
out OracleString rowid
)

ExecuteScalar:从数据库中检索一个值(例如,一个聚合值)作为 .NET Framework 数据类型。如:

CommandText = "SELECT COUNT(*) FROM Region";
Int32 count = (int32) ExecuteScalar();

ExecuteOracleScalar:从数据库中检索一个值(例如,一个聚合值)作为 Oracle 特定数据类型。 如:

command.CommandText = "SELECT COUNT(*) AS NumberOfRegions FROM Region";
count = command.ExecuteOracleScalar();
 

 

 

你可能感兴趣的:(oracle,数据,职场,休闲)