用C语言怎么实现与数据库的连接

用C语言怎么实现与数据库的连接,第1张

举个例子,连接SQL:

// 打开数据库

strDBClass.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0Data Source=%sJet OLEDB:Database Password=%s"), m_strUnEntryptMdbFilePath,m_strMDBPassword)

// 创建连接

HRESULT hr = m_pConnection.CreateInstance(_uuidof(Connection))

_ConnectionPtr m_pConnection->Open(m_strDBClass,_T(""),_T(""),adConnectUnspecified)

// 声明表单指针

_RecordsetPtr pBandRecordset

1.数据库连接:在config文件中的形式

<connectionStrings>

<add name="dbConnection" connectionString="Data Source=192.168.1.100Initial Catalog=NanFangMgrUser ID=saPassWord=sa" providerName="System.Data.SqlClient"/>

</connectionStrings>

2.在C#中调用:

System.Configuration.ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString

3.将上述连接串保存到文本文件

private string FILE_NAME = Application.StartupPath + "\\mytxtFile.txt"

private void WriteFile(string str)

{

StreamWriter sr

if (File.Exists(FILE_NAME)) //如果文件存在,则创建File.AppendText对象

{

sr = File.AppendText(FILE_NAME)

}

else//如果文件不存在,则创建File.CreateText对象

{

sr = File.CreateText(FILE_NAME)

}

sr.WriteLine(str)

sr.Close()

}

4.从文本文件中去内容

private String ReadTxtFile()

{

if (File.Exists(FILE_NAME)) //如果文件存在

{

String[] strs = System.IO.File.ReadAllLines(FILE_NAME)

return strs[strs.Length - 1]

}

return String.Empty

}

5.数据库连接,并 *** 作

5.1 查询

String ConnectionString=System.Configuration.ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString

public DataTable Query(String where)

{

String sql = String.Format("select * from mytable Where {0}", where.ToLower().Replace("update", "").Replace("delete", "").Replace("insert", "").Replace("", "").Replace("--", "").Replace("exec", ""))

try

{

SqlDataAdapter da = new SqlDataAdapter(sql, new SqlConnection(ConnectionString))

DataTable dt = new DataTable()

da.Fill(dt)

return dt

}

catch

{

return null

}

}

5.2 新增

public int New(Entities.mytable obj)

{

String sql = "insert into mytable(pkid,a,b,c) values(@pkid,@a,@b,@c)"

SqlConnection cn = new SqlConnection(ConnectionString)

SqlCommand cmd = new SqlCommand(sql, cn)

cmd.Parameters.AddWithValue("@a", obj.a)

cmd.Parameters.AddWithValue("@b", obj.b)

cmd.Parameters.AddWithValue("@c", obj.c)

cmd.Parameters.AddWithValue("@pkid",

String.Empty.Equals(obj.pkid) ? System.Guid.NewGuid().ToString() : obj.pkid)

try

{

if (cn.State != ConnectionState.Open)

cn.Open()

return cmd.ExecuteNonQuery()

}

catch

{

return -1

}

finally

{

if (cn.State != ConnectionState.Closed)

cn.Close()

}

}

5.3 编辑

public int Update(Entities.mytable obj)

{

String sql = "Update mytable Set a=@a,b=@b,c=@c Where pkid=@ObjectID"

SqlConnection cn = new SqlConnection(ConnectionString)

SqlCommand cmd = new SqlCommand(sql, cn)

cmd.Parameters.AddWithValue("@a", obj.a)

cmd.Parameters.AddWithValue("@b", obj.b)

cmd.Parameters.AddWithValue("@c", obj.c)

cmd.Parameters.AddWithValue("@pkid", obj.pkid)

try

{

if (cn.State != ConnectionState.Open)

cn.Open()

return cmd.ExecuteNonQuery()

}

catch

{

return -1

}

finally

{

if (cn.State != ConnectionState.Closed)

cn.Close()

}

}

5.4 删除

public int Del(String where)

{

String sql = String.Format("delete from mytable Where {0}", where.ToLower().Replace("update", "").Replace("delete", ""))

SqlConnection cn = new SqlConnection(ConnectionString)

SqlCommand cmd = new SqlCommand(sql, cn)

try

{

if (cn.State != ConnectionState.Open)

cn.Open()

return cmd.ExecuteNonQuery()

}

catch

{

return -1

}

finally

{

if (cn.State != ConnectionState.Closed)

cn.Close()

}

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9630368.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-30
下一篇 2023-04-30

发表评论

登录后才能评论

评论列表(0条)

保存