sql数据库到另一个pc-sql数据库

时间:2016-06-11 18:26:11

标签: c# sql

我正在使用此代码:

class Person extends Model {

    public $timestamps = false;
    public $guarded= ['id'];//Used in order to prevent filling from mass assignment

    public function city(){
        return $this->belongsTo('App\Models\City', 'city_id');
    }

    public static function savePerson($request){//Im sending a Request::all() from parameter
        $person = isset($request['id']) ? self::find($request['id']) : new self();
        $person->fill($request);//This won't work since my $request array is multi dimentional
        $person->save();
        return $person;
    }

    public function deeplyNestedAttributes()
    {
        return [
            'city_id',
            // another attributes
        ];
    }

    public function fill(array $attributes = [])
    {
        $attrs = $attributes;
        $nestedAttrs = $this->deeplyNestedAttributes();

        foreach ($nestedAttrs as $attr) {
            list($relationName, $relationAttr) = explode('_', $attr);

            if ( array_key_exists($relationName, $attributes) ) {

                if ( array_key_exists($relationAttr, $attributes[$relationName]) ) {

                    $attrs[$attr] = $attributes[$relationName][$relationAttr];
                }
            }
        }
        return parent::fill($attrs);
    }

}

我想从我的本地数据库中选择。

我试试这个,但它说错误的语法

SqlCommand cmd = new SqlCommand(" MERGE customermaster AS target USING Tamio.dbo.memberform AS source ON target.id = source.id WHEN MATCHED THEN UPDATE SET target.name = source.name WHEN NOT MATCHED THEN INSERT (id, name) VALUES (source.id, source.name);", con);

这也是我的本地连接

SqlCommand cmd = new SqlCommand(" MERGE customermaster AS target USING [local]Tamio.dbo.memberform AS source ON target.id = source.id WHEN MATCHED THEN UPDATE SET target.name = source.name WHEN NOT MATCHED THEN INSERT (id, name) VALUES (source.id, source.name);", con);

这是我的客户端电脑连接

SqlConnection con = new SqlConnection(@"Data Source=(local);Initial Catalog=Tamio;Integrated Security=True");

2 个答案:

答案 0 :(得分:1)

除非您指定服务器名称(如果您使用的是linked server),否则Tamio.dbo.memberform数据库是根据我的知识使用的本地数据库。如果您想使用其他服务器数据库,并且已经为此定义了Linked Server(使用sp_addlinkedserver),那么您可以使用

[server_name].Tamio.dbo.memberform

这是您的本地连接字符串,而您使用的是local DB

SqlConnection con = new SqlConnection(@"Data Source=(local);Initial Catalog=Tamio;Integrated Security=True");

然而,另一个是不同的服务器,没有Tamio DB。

答案 1 :(得分:0)

访问不同服务器的四部分语法import * as Bricks from 'bricks.js';仅适用于在所连接的服务器中配置链接服务器的情况。要使用ad-hoc连接,请使用OPENDATASOURCE https://msdn.microsoft.com/en-us/library/ms179856.aspx