在C#中,与数据库的连接的代码怎么写

2024-11-22 14:50:53
推荐回答(2个)
回答1:

//上传

    System.Web.HttpPostedFile myFile = this.Request.Files[0];

    //读取到数组里面
    System.IO.Stream mystream = myFile.InputStream;
    byte[] Buffer = new byte[myFile.ContentLength];
    mystream.Read(Buffer,0,myFile.ContentLength);

    //弊数打开数据库
    OracleConnection cn = new OracleConnection(ConfigurationSettings.AppSettings["sysDSN"]);
    cn.Open();

    //用参数方式写入数迹拿据库
    OracleCommand myComm = cn.CreateCommand();
 租州首   string sql = "insert into tmp(tmp_id,tmp_blob) values(tmp_seq.nextval,:tmp_blob)";
    myComm.CommandText = sql;
    myComm.Parameters.Add(":tmp_blob",OracleType.Blob,Buffer.Length).Value = Buffer;
    myComm.ExecuteNonQuery();

回答2:

public SqlConnection getCon() {
string sqlCon = "Data Source=(local);Database=数据名核咐;User id=sa;PWD=";
// string sqlCon = "橘氏备Provider=Microsoft.Jet.OleDb.4.0;Data Source=*.mdb"圆毁
SqlConnection myCon = new SqlConnection(sqlCon);
return myCon;
}