hojjjat
شنبه 22 تیر 1392, 12:55 عصر
سلام
من از این روش برای اتصال به پایگاه استفاده میکنم
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace bonga
{
class Class1
{
public static string connetionString = "Data Source=localhost;Initial Catalog=bonga;Integrated Security=True";
public static void setdata(string sqlstr)//DATA BASE CODE
{
SqlConnection mconnection = new SqlConnection(connetionString);
SqlCommand command = new SqlCommand(sqlstr, mconnection);
command.CommandType = CommandType.Text;
command.CommandText = sqlstr;
mconnection.Open();
try
{
command.ExecuteNonQuery();
}
catch (SqlException myerror)
{
MessageBox.Show("error conneting to data base:" + myerror.Message);
}
finally
{
mconnection.Close();
mconnection.Dispose();
}
}
public static DataTable getdata(string sqlcommand)
{
SqlConnection mconnection = new SqlConnection(connetionString);
SqlCommand command = new SqlCommand(sqlcommand, mconnection);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
DataTable table = new DataTable();
try
{
adapter.Fill(table);
}
catch (SqlException myerror)
{
MessageBox.Show("error conneting to data base:" + myerror.Message);
}
finally
{
mconnection.Dispose();
}
return table;
}
}
}
و در برنامه اصلی برای ذخیره کردن در پایگاه
private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
MessageBox.Show("لطفا نام را وارد کنید", "اخطار", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
Class1.setdata("insert into bonga (noee,name,address,pish,ejaree) values( '"+st+"','" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')");
ejare_Load(sender, e);
textBox1.Text = null;
textBox2.Text = null;
textBox3.Text = null;
textBox4.Text = null;
}
این روش خوبی برا من جواب مبده اما یکی از دوستان گفت این روش خیلی قدیمی هست و بدرد نمیخوره
میخواستم بپرسم چرا؟؟ مگه مشکلش چیه؟؟
الان روش های جدید و بهتر کدومن؟؟
اگه میشه لینک اموزشی قرار دهید..
یک سوال دیگه برا جستجو در پایگاه باید چکار کنم؟؟
من از این روش برای اتصال به پایگاه استفاده میکنم
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace bonga
{
class Class1
{
public static string connetionString = "Data Source=localhost;Initial Catalog=bonga;Integrated Security=True";
public static void setdata(string sqlstr)//DATA BASE CODE
{
SqlConnection mconnection = new SqlConnection(connetionString);
SqlCommand command = new SqlCommand(sqlstr, mconnection);
command.CommandType = CommandType.Text;
command.CommandText = sqlstr;
mconnection.Open();
try
{
command.ExecuteNonQuery();
}
catch (SqlException myerror)
{
MessageBox.Show("error conneting to data base:" + myerror.Message);
}
finally
{
mconnection.Close();
mconnection.Dispose();
}
}
public static DataTable getdata(string sqlcommand)
{
SqlConnection mconnection = new SqlConnection(connetionString);
SqlCommand command = new SqlCommand(sqlcommand, mconnection);
SqlDataAdapter adapter = new SqlDataAdapter();
adapter.SelectCommand = command;
DataTable table = new DataTable();
try
{
adapter.Fill(table);
}
catch (SqlException myerror)
{
MessageBox.Show("error conneting to data base:" + myerror.Message);
}
finally
{
mconnection.Dispose();
}
return table;
}
}
}
و در برنامه اصلی برای ذخیره کردن در پایگاه
private void button1_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox1.Text))
MessageBox.Show("لطفا نام را وارد کنید", "اخطار", MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);
Class1.setdata("insert into bonga (noee,name,address,pish,ejaree) values( '"+st+"','" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')");
ejare_Load(sender, e);
textBox1.Text = null;
textBox2.Text = null;
textBox3.Text = null;
textBox4.Text = null;
}
این روش خوبی برا من جواب مبده اما یکی از دوستان گفت این روش خیلی قدیمی هست و بدرد نمیخوره
میخواستم بپرسم چرا؟؟ مگه مشکلش چیه؟؟
الان روش های جدید و بهتر کدومن؟؟
اگه میشه لینک اموزشی قرار دهید..
یک سوال دیگه برا جستجو در پایگاه باید چکار کنم؟؟