PDA

View Full Version : سوال: نحوه کار با SQL Dependency در سی شارپ



nilmil_nil
یک شنبه 16 مهر 1391, 18:44 عصر
سلام دوستان !!!
از بچه ها کسی با SQL Dependency کار کرده؟
اگه آره یه منبع یا یه مثال کوچیک یا توضیح ..!!!
ممنون

ali_habibi1384
یک شنبه 16 مهر 1391, 19:09 عصر
چند وقت پيش درباره همين sqlDependency ميخواستم يه آموزش كامل بذارم اما دوستان استقبال نكردند.
علي ايهال اين نمونه برنامه رو براتون ميذارم اميدوارم بدردتون بخوره. اين يه نمونه برنامه چت بين دو تا كامپيوتر هست كه داده ها رو ميفرستن به سرور و توسط sqlDependency اطلاعاتش رو ميخونه .ديتابيسش هم براتون script كردم تا مشكلي با ورژنش نداشته باشين.

nilmil_nil
دوشنبه 17 مهر 1391, 08:23 صبح
ممنون
کاش آموزش رو میذاشتین
و اینم بگم که نیازی به استقبال کسی نیست
وقتی کسی به این نیازی پیدا نکنه مسلما سراغش نمیره.
اگه جایی هم اسمشو ببینه ممکنه یه نگاه بندازه ببینه کارایشش به دردش نمیخوره پس ولش می کنه کاریش نداره
ما وظیفه داریم هرچیو بلدیم تا جایی که ممکنه انتقال بدیم
بهر حال این نظر من بود
اگه تونستید یه توضیح کوچیک بذارین
ممنون میشم

hyperboy743
چهارشنبه 22 دی 1395, 11:47 صبح
با سلام خدمت دوستان توابعی که مربوط به SQL-Dependency بود رو من به این صورت نوشتم و جواب هم گرفتم

try
{
if (!DoesUserHavePermission())
return;

SqlConnection con=new SqlConnection();
string connectionString=TestSql.Properties.Settings.Defau lt.TestSqlConnectionString;


// You must stop the dependency before starting a new one.
// You must start the dependency when creating a new one.
SqlDependency.Stop(connectionString);
SqlDependency.Start(connectionString);

SqlConnection cn = new SqlConnection(connectionString);
SqlDataAdapter da = new SqlDataAdapter();

SqlCommand cmd = new SqlCommand();
string query1 = "SELECT Id,Name,Family,Address,Email,Phone,flag FROM dbo.TblUsers";
cmd.CommandType = CommandType.Text;
cmd.CommandText = query1;
cmd.Connection = cn;
da.SelectCommand = cmd;
cmd.CommandTimeout = 30;
cmd.Notification = null;

// creates a new dependency for the SqlCommand
SqlDependency dep = new SqlDependency(cmd);
// creates an event handler for the notification of data
// changes in the database.
// NOTE: the following code uses the normal .Net capitalization methods, though
// the forum software seems to change it to lowercase letters
dep.OnChange += new OnChangeEventHandler(dep_onchange);

cn.Open();

DataTable dtuser = new DataTable("TblUsers");
//using (SqlDataReader dr = cmd.ExecuteReader())
//{
// while (dr.Read())
// {

da.Fill(dtuser);
//dtuser.Load(dr);

dataGridView1.DataSource = dtuser;
//dgw.ret



// }

//}


}

catch (Exception exception)
{
throw new Exception(exception.Message);
}





try
{
SqlClientPermission clientPermission = new SqlClientPermission(PermissionState.Unrestricted);

// will throw an error if user does not have permissions
clientPermission.Demand();

return true;
}
catch
{
return false;
}



void dep_onchange(object sender, SqlNotificationEventArgs e)
{
// this event is run asynchronously so you will need to invoke to run on UI thread(if required).
if (this.InvokeRequired)

dataGridView1.BeginInvoke(new MethodInvoker(binddgw));
else
binddgw();

// this will remove the event handler since the dependency is only for a single notification
SqlDependency dep = sender as SqlDependency;

// NOTE: the following code uses the normal .Net capitalization methods, though
// the forum software seems to change it to lowercase letters
dep.OnChange -= new OnChangeEventHandler(dep_onchange);
}

private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'testSqlDataSet.TblUsers' table. You can move, or remove it, as needed.
//this.tblUsersTableAdapter.Fill(this.testSqlDataSet .TblUsers);
// binddgw();

try
{
binddgw();
}
catch (Exception exception)
{
MessageBox.Show(exception.Message);
}

button2.Enabled = false;
}