我創建了單獨的類稱爲連接,我已經回到了SqlConnection
這樣的:如何在兩個單獨的類之間進行通信?
class connection
{
public SqlConnection Functions()
{
string connetionString = null;
SqlConnection cnn;
Boolean flag = false;
connetionString = "Data Source=(LocalDB)\\v11.0;AttachDbFilename=|DataDirectory|\\Database1.mdf;Integrated Security=True";
cnn = new SqlConnection(connetionString);
return cnn;
}
}
而且我想使用另一種單獨的類返回的連接,但它似乎錯誤,我已經編碼如下
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using WindowsFormsApplication1.deepak;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection cnn ;
connection c = new connection();
cnn = new connection.Function();
try
{
cnn.Open();
MessageBox.Show ("Connection Open ! ");
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
}
}
}
什麼是錯誤? – dotnetom