我是.Net和C#的新手,我一直在努力探索如何利用在代码的一部分中创建的SQL连接并在另一部分中使用它。我的表格上有2个按钮。一个连接到数据库,另一个插入表。插入表格时如何使用连接变量?
我希望这是有道理的。感谢
namespace SQL
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnConnect_Click(object sender, EventArgs e)
{
SqlOperations connect = new SqlOperations();
connect.connectToSQL("server=localhost\\SQLExpress;", "database=Cromwell; ");
}
private void btnAddToDatabase_Click(object sender, EventArgs e)
{
SqlOperations addToTable = new SqlOperations();
addToTable.InsertToTable("InputDir", "C:\\");
}
}
public class SqlOperations
{
public bool connectToSQL(string sqlHost, string database)
{
SqlConnection SqlConnect = new SqlConnection("user id=userid;" +
"password=validpassword;" + sqlHost +
"Trusted_Connection=yes;" +
database + "connection timeout=30");
try
{
SqlConnect.Open();
MessageBox.Show("Connected to SQL Express");
return true;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return false;
}
}
public bool InsertToTable(string columnName, string value)
{
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = **SqlConnect**; // THIS BIT COMPLAINS
myCommand.CommandText = "INSERT INTO Config (" + columnName + ") " +
"Values ('" + value + "')";
}
}
}
答案 0 :(得分:3)
解决方案1:您可以将连接字符串创建为constant
字符串变量,并使用class
名称从您需要的名称访问它,因为常量变量是隐式{{1 (你可以将它们作为全局变量访问)
试试这个:
static
访问时:
Class MyConnectionString
{
const string strCon="user id=userid;password=validpassword;
server=localhost\\SQLExpress;database=Cromwell;
Trusted_Connection=yes;connection timeout=30";
}
解决方案2:
在SqlConnection scon=new SqlConnection(MyConnectionString.strCon);
中创建连接字符串并访问它。
Configuration file
在需要时使用它:
<connectionStrings>
<add name="myConString"
connectionString="user id=userid;password=validpassword;
server=localhost\\SQLExpress;database=Cromwell;
Trusted_Connection=yes;connection timeout=30" />
</connectionStrings>
答案 1 :(得分:0)
点击此链接http://www.codeproject.com/Articles/4416/Beginners-guide-to-accessing-SQL-Server-through-C。它是使用c#.NET访问SQL数据库的初学者指南。
您还可以在web.config或app.config中添加连接字符串,然后从c#代码中访问它。
C# // Add a reference at the top of your code file using System.Configuration; // Within the code body set your variable string cs = ConfigurationManager.ConnectionStrings["connectionStringName"].ConnectionString;
VB ' Add a reference at the top of your code file Imports System.Configuration ' Within the code body set your variable Dim cs as String = ConfigurationManager.ConnectionStrings("connectionStringName").ConnectionString
显然请记住在web.config
中添加(使用您自己的连接字符串)<connectionStrings>
<add name="ConnStringDb1" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />
答案 2 :(得分:0)
我喜欢你定义了一个连接SQL的类。您可以使用该类来管理SQL连接的生命周期,这是一件好事。如果它也处理连接凭证也会很好,这样你的呼叫者就不必知道它是什么。怎么样:
public class SqlOperations
{
private SqlConnection Connect()
{
... Get SQL credentials here
... Open and return connection here
}
public bool InsertToTable(string columnName, string value)
{
using (var conn = Connect())
{
using (SqlCommand myCommand = new SqlCommand())
{
myCommand.Connection = conn;
... do your myCommand stuff here
}
}
}
}
然后在你的表格中放弃连接到数据库按钮 - 它是为你管理的!您只需要使用此事件插入按钮:
private void btnAddToDatabase_Click(object sender, EventArgs e)
{
SqlOperations addToTable = new SqlOperations();
addToTable.InsertToTable("InputDir", "C:\\");
}