当前位置:Gxlcms > 数据库问题 > C#数据库连接

C#数据库连接

时间:2021-07-01 10:21:17 帮助过:24人阅读

SQLMANAGE

数据库连接管理

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace tmp2013
{
class SqlManage
{
public static string cnnstring = "Server=wit;Database=test;Uid=sa;Pwd=123456;";
public static SqlConnection cnn = null;

public static void TableChange(string sql)
{
try
{
SqlCommand cmd = new SqlCommand(sql,SqlManage.cnn);
if (cmd.ExecuteNonQuery() > 0)
{
MessageBox.Show("操作成功!");
}
else
{
MessageBox.Show("操作失败!\n检查数据库是否有该记录");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

public static DataTable TableSelect(string sql)
{
try
{
DataTable table = new DataTable();
SqlDataAdapter sdp = new SqlDataAdapter(sql,SqlManage.cnn);
sdp.Fill(table);
return table;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
}
}
}

 

 

主函数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace tmp2013
{
static class Program
{
// string cnnstring = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=emp;Data Source=hubo-pc";
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
try
{
SqlManage.cnn = new SqlConnection(SqlManage.cnnstring);
SqlManage.cnn.Open();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
Application.Exit();
return;
}
Application.Run(new FormMain());

SqlManage.cnn.Close();
}
}
}

C#数据库连接

标签:exce   程序   tables   tmp   namespace   window   cti   函数   text   

人气教程排行