PDA

View Full Version : اتصال به دیتا بیس



arams7474
پنج شنبه 23 خرداد 1392, 16:32 عصر
سلام به دوستان
من یک برنامه دفترچه تلفن نوشتم ولی در اتصالش به دیتابیس دچار مشکل شدم کمک فوری باید پروژه را همین امشب تحویل بدم با تشکر . کد مورد نظر :


namespace Telephonbook
{
public partial class frmedite : Form
{
public string path = "Data Source=HOSPITAL-AS;Initial Catalog=Telephonbookdb;Persist Security Info=True;User ID=za;Password=1263";
public frmedite()
{
InitializeComponent();
}
private void btnjostojo_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection(path);
SqlCommand cmd = new SqlCommand("Select * from tblTelephon", cnn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();

cnn.ConnectionString = path;
cmd.Connection = cnn;
da.SelectCommand = cmd;
cnn.Open();

if (txtjostojo.Text != "")
{
cmd.CommandText = "Select * from tblTelephon WHERE name =@name";
cmd.Parameters.AddWithValue("@name", txtjostojo.Text);
}
da.SelectCommand = cmd;
da.Fill(dt);
dataGridView1.DataSource = dt;
groupBox1.Enabled = true;
txtname.Text = dt.Rows[0][1].ToString();
txtfamily.Text=dt.Rows[0][2].ToString();
txtcity.Text = dt.Rows[0][3].ToString();
txtmaile.Text = dt.Rows[0][4].ToString();
txtcode.Text = dt.Rows[0][5].ToString();
txtmobile.Text = dt.Rows[0][6].ToString();
txtphpne.Text = dt.Rows[0][7].ToString();


cnn.Close();

khokhan
پنج شنبه 23 خرداد 1392, 17:39 عصر
سلام به دوستان
من یک برنامه دفترچه تلفن نوشتم ولی در اتصالش به دیتابیس دچار مشکل شدم کمک فوری باید پروژه را همین امشب تحویل بدم با تشکر . کد مورد نظر :


namespace Telephonbook
{
public partial class frmedite : Form
{
public string path = "Data Source=HOSPITAL-AS;Initial Catalog=Telephonbookdb;Persist Security Info=True;User ID=za;Password=1263";
public frmedite()
{
InitializeComponent();
}
private void btnjostojo_Click(object sender, EventArgs e)
{
SqlConnection cnn = new SqlConnection(path);
SqlCommand cmd = new SqlCommand("Select * from tblTelephon", cnn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();

cnn.ConnectionString = path;
cmd.Connection = cnn;
da.SelectCommand = cmd;
cnn.Open();

if (txtjostojo.Text != "")
{
cmd.CommandText = "Select * from tblTelephon WHERE name =@name";
cmd.Parameters.AddWithValue("@name", txtjostojo.Text);
}
da.SelectCommand = cmd;
da.Fill(dt);
dataGridView1.DataSource = dt;
groupBox1.Enabled = true;
txtname.Text = dt.Rows[0][1].ToString();
txtfamily.Text=dt.Rows[0][2].ToString();
txtcity.Text = dt.Rows[0][3].ToString();
txtmaile.Text = dt.Rows[0][4].ToString();
txtcode.Text = dt.Rows[0][5].ToString();
txtmobile.Text = dt.Rows[0][6].ToString();
txtphpne.Text = dt.Rows[0][7].ToString();


cnn.Close();



سلام

این یخط رو بارشته اتصال خودتون عوض کنین

public string path = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirector y|\Telephonbookdb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"


البته حتما که فایل بانک رو توی پوشه debug پروژه گذاشته باشین

arams7474
پنج شنبه 23 خرداد 1392, 20:02 عصر
سلام

این یخط رو بارشته اتصال خودتون عوض کنین

public string path = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirector y|\Telephonbookdb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"


البته حتما که فایل بانک رو توی پوشه debug پروژه گذاشته باشین



با تشکر از شما دوست عزیز قسمت اضافه کردن شماره تلفن با همین کد کار می کنه من مستقیما به دیتابیس وصلش کردم

vB.N3T
پنج شنبه 23 خرداد 1392, 23:13 عصر
شاید این کد کمکتون کنه

private void button1_Click(object sender, EventArgs e)
{
{
SqlConnection con1 = new SqlConnection("server=localhost;database=clup;integrated security=true");
SqlDataAdapter da = new SqlDataAdapter("select * from tbl_sabt_film where id = @id", con1);
da.SelectCommand.Parameters.AddWithValue("@id", textBox1.Text);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
MessageBox.Show("این مشخصه قبلا ثبت شده است");
textBox1.Focus();
}
else
{
SqlCommand com1 = new SqlCommand("insert into tbl_sabt_film(id,name,keshvar,sabk,sal,tedad,gheym at) values(@id,@name,@keshvar,@sabk,@sal,@tedad,@gheym at", con1);
com1.Parameters.AddWithValue("@id", textBox1.Text);
com1.Parameters.AddWithValue("@name", textBox2.Text);
com1.Parameters.AddWithValue("@keshvar", textBox3.Text);
com1.Parameters.AddWithValue("@sabk", textBox4.Text);
com1.Parameters.AddWithValue("@sal", maskedTextBox1.TextMaskFormat);
com1.Parameters.AddWithValue("@tedad", textBox6.Text);
com1.Parameters.AddWithValue("@gheymat", textBox7.Text);
con1.Open();
com1.ExecuteNonQuery();
con1.Close();
MessageBox.Show("اطلاعات با موفقیت ثبت گردید");
}
}
}