我该如何连接数据库

时间:2013-09-09 19:00:50

标签: database-connection

我使用此功能但无法连接DataBase

  conn1.Open();
    using (OracleCommand crtCommand = new OracleCommand);

2 个答案:

答案 0 :(得分:4)

从应用程序连接到Oracle

     string command =  "Enter your command";  
        OracleConnection orclecon;
             orclecon = new OracleConnection(connection);
  orclecon.Open();

将此命令用于选择命令:

 DataSet ds = new DataSet();
 using (OracleDataAdapter Oda = new OracleDataAdapter(command, orclecon))
                {
                    Oda.Fill(ds);
                }

并将其用于插入/更新/删除命令:

//used for Oracle command (insert,update,delete) if number of rows that affected >0 return true else return false

using (OracleCommand orclcommand = new OracleCommand(command, orclecon))
                        {
                            int n = orclcommand.ExecuteNonQuery();
                            if (n > 0)
                                return true;
                            else
                                return false;
                    }
    orclecon.Close();

答案 1 :(得分:2)

使其动态使用;

    string sentence = "";
    string formatprototype = "";//This will hold the string to be formatted.
    string output="";

    public void SearchString()
    {
        string pattern = @".*[ ]+?[\""]{1}(?<String>[a-zA-Z0-9_]*)[\""]{1}[ ]+?MINVALUE[ ]*(?<MinValue>[-?\d]*)[ ]*MAXVALUE[ ]*(?<MaxValue>[\d]*)[ ]+?[INCREMENT]*[ ]+?[BY]*[ ]+?(?<IncrementBy>[\d]*)[ ]+?[START]*[ ]+?[WITH]*[ ]+?(?<StartWith>[\d]*)[ ]+?[CACHE]*[ ]+?(?<Cache>[\d]*)\s+?";
        Regex regex = new Regex(pattern);
        Match match = regex.Match(sentence);
        Group @string = match.Groups[1];
        Group minvalue = match.Groups[2];
        Group maxvalue = match.Groups[3];
        Group incrementby = match.Groups[4];
        Group startswith = match.Groups[5];
        Group cache = match.Groups[6];
        formatprototype = @"CREATE SEQUENCE ""{0}"" MINVALUE {1} MAXVALUE {2} INCREMENT BY {3} START WITH {4} CACHE {5} NOORDER NOCYCLE";
        if (minvalue.Value.StartsWith("-"))
        {
            output = string.Format(formatprototype, @string, minvalue, maxvalue, incrementby, maxvalue, cache);
        }
        else if (!minvalue.Value.StartsWith("-"))
        {
            output = string.Format(formatprototype, @string, minvalue, maxvalue, incrementby, minvalue, cache);
        }
        MessageBox.Show(output);
    }

假设SearchString()是您正在执行此操作的函数。并确保将从数据库中提取的每个字符串分配给sentence。尝试并回复它是否有效